tags:

views:

313

answers:

4

Is it possible to include the day suffix when formatting a date using DateTime.ToString()?

For example I would like to print the date in the following format - Monday 27th July 2009. However the closest example I can find using DateTime.ToString() is Monday 27 July 2009.

Can I do this with DateTime.ToString() or am I going to have to fall back to my own code?

+6  A: 

I don't think there is a built in way, but here is someone who wrote something just for this.

How To Append 'st', 'nd', 'rd' or 'th' To Day Numbers in Dates

Stan R.
A: 

As a reference I always use/refer to SteveX String Formatting and there doesn't appear to be any "th" in any of the available variables but you could easily build a string with

string.Format("{0:dddd dd}{1} {0:MMMM yyyy}", DateTime.Now, (?));

You would then have to supply a "st" for 1, "nd" for 2, "rd" for 3, and "th" for all others and could be in-lined with a "? :" statement.

(DateTime.Now.Day == 1) ? "st" : (DateTime.Now.Day == 2) ? "nd" : (DateTime.Now.Day == 3) ? "rd" : "th"

Bryan Bailliache
A: 

Think you're on your own there I'm afraid. There doesn't seem to be any support for it in .NET

Fiona Holder
A: 

in the MSDN documentation there is no reference to a culture that could convert that 17 into 17th. so You should do it manually via code-behind.Or build one...you could build a function that does that.

public string CustomToString(this DateTime date)
    {
        string dateAsString = string.empty;
        <here wright your code to convert 17 to 17th>
        return dateAsString;
    }
GxG