Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting day suffix when using DateTime.ToString()

Tags:

date

c#

.net

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?

like image 309
Craig Bovis Avatar asked Jan 12 '10 17:01

Craig Bovis


People also ask

How do I change the date format in ToString?

To do this, you use the "MM/yyyy" format string. The format string uses the current culture's date separator. Getting a string that contains the date and time in a specific format. For example, the "MM/dd/yyyyHH:mm" format string displays the date and time string in a fixed format such as "19//03//2013 18:06".

What is string date/time format?

Date and time format strings A date and time format string is a string of text used to interpret data values containing date and time information. Each format string consists of a combination of formats from an available format type. Some examples of format types are day of week, month, hour, and second.

How do I change the DateTime format in Visual Studio?

In "Control Panel\All Control Panel Items\Region and Language", on the "Formats" tab, click "Additional settings..." button. Visual Studio 2008 debugger formats dates using "Short date:" from the "Date" tab of "Customize Format".


1 Answers

Another option using switch:

string GetDaySuffix(int day) {     switch (day)     {         case 1:         case 21:         case 31:             return "st";         case 2:         case 22:             return "nd";         case 3:         case 23:             return "rd";         default:             return "th";     } } 
like image 98
Lazlow Avatar answered Oct 05 '22 07:10

Lazlow