Getting day suffix when using DateTime.ToString()

Viewed 68819

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?

20 Answers

As a reference I always use/refer to [SteveX String Formatting] 1 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.

var now = DateTime.Now;
(now.Day % 10 == 1 && now.Day % 100 != 11) ? "st"
: (now.Day % 10 == 2 && now.Day % 100 != 12) ? "nd"
: (now.Day % 10 == 3 && now.Day % 100 != 13) ? "rd"
: "th"

For those who are happy to use external dependencies (in this case the fantastic Humanizr .net), it's as simple as

dateVar.Day.Ordinalize(); \\ 1st, 4th etc depending on the value of dateVar

Simpler answer using a switch expression (since C# 8):

var daySuffix = dateTime.Day switch {
                    1 or 21 or 31 => "st",
                    2 or 22 => "nd",
                    3 or 23 => "rd",
                    _ => "th",
                };

For what its worth here is my final solution using the below answers

     DateTime dt = DateTime.Now;
        string d2d = dt.ToString("dd").Substring(1); 

        string suffix =
       (dt.Day == 11 || dt.Day == 12 || dt.Day == 13) ? "th"
       : (d2d == "1") ? "st"
       : (d2d == "2") ? "nd"
       : (d2d == "3") ? "rd"
       : "th";


        Date.Text = DateTime.Today.ToString("dddd d") + suffix + " " + DateTime.Today.ToString("MMMM") + DateTime.Today.ToString(" yyyy"); 

Check out humanizr: https://github.com/Humanizr/Humanizer#date-time-to-ordinal-words

new DateTime(2015, 1, 1).ToOrdinalWords() => "1st January 2015"
new DateTime(2015, 2, 12).ToOrdinalWords() => "12th February 2015"
new DateTime(2015, 3, 22).ToOrdinalWords() => "22nd March 2015"
// for English US locale
new DateTime(2015, 1, 1).ToOrdinalWords() => "January 1st, 2015"
new DateTime(2015, 2, 12).ToOrdinalWords() => "February 12th, 2015"
new DateTime(2015, 3, 22).ToOrdinalWords() => "March 22nd, 2015"

I realized right after posting this that @Gumzle suggested the same thing, but I missed his post because it was buried in code snippets. So this is his answer with enough code that someone (like me) quickly scrolling through might see it.

A solution you can use in a razor template. It's probably not the most elegant way but it's quick and it works

@Model.Pubdate.ToString("dddd, d'th' MMMM yyyy").Replace("1th","1st").Replace("2th", "2nd").Replace("3th", "3rd").Replace("11st", "11th").Replace("12nd", "12th").Replace("13rd", "13th")

ToString("dddd, d'th' MMMM yyyy") adds "th" after the day number, eg "Tuesday, 31th May 2022". Then use a set of string replacements to change 1th, 2th and 3th to 1st, 2nd and 3rd, and another set to change 11st, 12nd and 13rd back to 11th, 12th and 13th.

string datestring;    
// datestring = DateTime.Now.ToString("dd MMMM yyyy"); // 16 January 2021

    // code to add 'st' ,'nd', 'rd' and 'th' with day of month 
    // DateTime todaysDate = DateTime.Now.Date; // enable this line for current date 
    DateTime todaysDate = DateTime.Parse("01-13-2021"); // custom date to verify code // 13th January 2021
    int day = todaysDate.Day;
    string dateSuffix;

    if(day==1 || day==21 || day==31){
        dateSuffix= "st";
    }else if(day==2 || day==22 ){
        dateSuffix= "nd";
    }else if(day==3 || day==23 ){
        dateSuffix= "rd";
    }else{
        dateSuffix= "th";
    }
    datestring= day+dateSuffix+" "+todaysDate.ToString("MMMM")+" "+todaysDate.ToString("yyyy");
    Console.WriteLine(datestring);

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;
    }
Related