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.)

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.