Hire Me

Including a Day Suffix when formatting a Date in C#

How to format a datetime in C# with a suffix on the day

Posted on Saturday, July 5, 2014 at 11:17 PM into csharp code by Steve Woods

One formatting option for the DateTime object in C# that is missing is the ability to display the correct suffix for a given date, as in 4th January - Usually the best you can hope for is something like 4 January which looks crap.

Here's a quick extension method I've written which will automatically calculate it for you and allow you to format the date as normal.

The Code

Usage

Call the code like this:

DateTime myDate = new DateTime(2000,1,1,0,0,0);
Debug.Print(myDate.ToStringWithSuffix("dd[$suffix] mmm yyyy"));

Job done :)

 

 

 

;

Comments

Please consider what you post!
Chillax if you're angry.