Convert dates into an ordinal format (as in August 20th, 2020) in C#

Convert dates into an ordinal format

This example shows how you can Convert dates into an ordinal format as in August 20th, 2020. The example Convert an integer into an ordinal (1st, 2nd, 3rd) in C# explains how to find the ordinal extension for an integer. This example uses a similar technique for dates.

The following code uses the previous example’s ToOrdinal method to make a DateTime extension method.

// Format a date as in "August 20th, 2020."
public static string ToOrdinal(this DateTime value)
    return value.ToString("MMMM") + " " +
        value.Day + value.Day.ToOrdinal() +
        ", " + value.Year;

This example uses an American date format. If you use some other format, you may want to change the code to produce a different result such as “20th August 2020.” (Of course if you just want the format “20 August 2010,” then you can skip the whole issue.)

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in parsing, strings and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *