BLOG.CSHARPHELPER.COM: Find the ordinal representation of a date as in "August 20th, 2010" in C#
Find the ordinal representation of a date as in "August 20th, 2010" in C#
The example Find the ordinal representation of an integer (1st, 2nd, 3rd) in C# explains how to find the ordinal extension for an integer.
The following code uses that to make a DateTime extension method that returns a date in an ordinal format such as "August 20th, 2010."
// Format a date as in "August 20th, 2010." public static string OrdinalFormat(this DateTime value) { return value.ToString("MMMM") + " " + value.Day + value.Day.OrdinalExtension() + ", " + 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 2010." (Of course if you just want "20 August 2010" then you can skip the whole issue.)
Comments