SunQuest
           .Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old February 18th, 2008, 09:49 AM
photopygmy photopygmy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 16 photopygmy User rank is Corporal (100 - 500 Reputation Level)photopygmy User rank is Corporal (100 - 500 Reputation Level)photopygmy User rank is Corporal (100 - 500 Reputation Level)photopygmy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 3 m 48 sec
Reputation Power: 0
Format a date to include english date suffix

Hi,

Can anyone tell me the correct parameter to format a date with english date suffixes in c#.

(e.g 3rd February)

I can't seem to find a decent example from seraching the net.

Many Thanks

Reply With Quote
  #2  
Old February 18th, 2008, 11:07 AM
MBirchmeier's Avatar
MBirchmeier MBirchmeier is offline
I <3 ASCII
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Wishing i was still at... The Ohio State University
Posts: 2,238 MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level)MBirchmeier User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 1 h 21 m 13 sec
Reputation Power: 708
Send a message via AIM to MBirchmeier Send a message via Yahoo to MBirchmeier
There is no built in function for that. Basically the best way to do it is to write something yourself to pump out the suffix, then format the date/time string accordingly.

-Mbirchmeier
__________________
I have noticed that the devshed spell check sugggests that MBirchmeier is a misspelling for 'bitchier'.

Apparently even computers have freudian slips.

0x4279 7465 204D 6521

Reply With Quote
  #3  
Old February 18th, 2008, 12:23 PM
photopygmy photopygmy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 16 photopygmy User rank is Corporal (100 - 500 Reputation Level)photopygmy User rank is Corporal (100 - 500 Reputation Level)photopygmy User rank is Corporal (100 - 500 Reputation Level)photopygmy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 h 3 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by MBirchmeier
There is no built in function for that. Basically the best way to do it is to write something yourself to pump out the suffix, then format the date/time string accordingly.

-Mbirchmeier


o.k. Thanks very much for the reply

Reply With Quote
  #4  
Old April 22nd, 2008, 10:42 AM
Hainesy Hainesy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 1 Hainesy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 22 sec
Reputation Power: 0
I wrote the following code to do exactly this...


Code:
// Use "~" for English Date Suffix
public static string CustomDateFormat(string format, DateTime date)
{
    string[] arr = format.Split('~');
    string suffix = GetDateSuffix(date);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < arr.Length; i++)
    {
        sb.Append(string.Format("{0:" + arr[i] + "}", date));
        if (i < arr.Length - 1)
        {
            sb.Append(suffix);
        }
    }
    return sb.ToString();
}

public static string GetDateSuffix(DateTime date)
{
    string day = date.Day.ToString();
    if (day.EndsWith("1"))
    {
        return day.StartsWith("1") && date.Day != 1 ? "th" : "st";
    }
    else if (day.EndsWith("2"))
    {
        return day.StartsWith("1") ? "th" : "nd";
    }
    else if (day.EndsWith("3"))
    {
        return day.StartsWith("1") ? "th" : "rd";
    }
    return "th";
}


You can use it as follows:
Code:
CustomDateFormat("dddd d~ MMMM yyyy", DateTime.Now)

Reply With Quote
  #5  
Old May 9th, 2008, 04:02 AM
rufdraft rufdraft is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 rufdraft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 8 sec
Reputation Power: 0
Thumbs up Brilliant!

Thanks Hainesy,I am using it on my Project.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Format a date to include english date suffix


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway