Getting the day suffix using DateTime.ToString ()

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, July 27, 2009. However, the closest example I can find with DateTime.ToString () is Monday July 27, 2009.

Can I do this with DateTime.ToString () or will I have to go back to my own code?

+70
date c #
Jan 12 '10 at 17:10
source share
15 answers

As a reference, I always use / reference SteveX String Formatting, and none of the available variables seem to have "th", but you can easily build a string with

string.Format("{0:dddd dd}{1} {0:MMMM yyyy}", DateTime.Now, (?)); 

Then you will need to specify "st" for 1, "nd" for 2, "rd" for 3 and "th" for everyone else, and you can use the operator "?:"

 var now = DateTime.Now; (now.Day % 10 == 1 && now.Day != 11) ? "st" : (now.Day % 10 == 2 && now.Day != 12) ? "nd" : (now.Day % 10 == 3 && now.Day != 13) ? "rd" : "th" 
+53
Jan 12
source share

Another option using a 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"; } } 
+219
Feb 03 2018-12-12T00:
source share

Using several extension methods:

 namespace System { public static class IntegerExtensions { public static string ToOccurrenceSuffix(this int integer) { switch (integer % 100) { case 11: case 12: case 13: return "th"; } switch (integer % 10) { case 1: return "st"; case 2: return "nd"; case 3: return "rd"; default: return "th"; } } } public static class DateTimeExtensions { public static string ToString(this DateTime dateTime, string format, bool useExtendedSpecifiers) { return useExtendedSpecifiers ? dateTime.ToString(format) .Replace("nn", dateTime.Day.ToOccurrenceSuffix().ToLower()) .Replace("NN", dateTime.Day.ToOccurrenceSuffix().ToUpper()) : dateTime.ToString(format); } } } 

Using:

 return DateTime.Now.ToString("dddd, dnn MMMM yyyy", useExtendedSpecifiers: true); // Friday, 7th March 2014 

Note. The integer extension method can be used for any number, and not just from 1 to 31. For example,

 return 332211.ToOccurrenceSuffix(); // th 
+28
Mar 07 '14 at 9:10
source share

Another option is to use the modulo operator :

 public string CreateDateSuffix(DateTime date) { // Get day... var day = date.Day; // Get day modulo... var dayModulo = day%10; // Convert day to string... var suffix = day.ToString(CultureInfo.InvariantCulture); // Combine day with correct suffix... suffix += (day == 11 || day == 12 || day == 13) ? "th" : (dayModulo == 1) ? "st" : (dayModulo == 2) ? "nd" : (dayModulo == 3) ? "rd" : "th"; // Return result... return suffix; } 

Then you would call the above method, passing the DateTime object as a parameter, for example:

 // Get date suffix for 'October 8th, 2019': var suffix = CreateDateSuffix(new DateTime(2019, 10, 8)); 

For more information on the DateTime constructor, please see the Microsoft Docs Page .

+12
Jan 28 2018-12-12T00:
source share

Here is an extended version, including 11, 12, and 13:

 DateTime dt = DateTime.Now; string d2d = dt.ToString("dd").Substring(1); string daySuffix = (dt.Day == 11 || dt.Day == 12 || dt.Day == 13) ? "th" : (d2d == "1") ? "st" : (d2d == "2") ? "nd" : (d2d == "3") ? "rd" : "th"; 
+7
Oct 11 '11 at 16:00
source share

UPDATE

NuGet Package:
https://www.nuget.org/packages/DateTimeToStringWithSuffix

Example:
https://dotnetfiddle.net/zXQX7y

Support:
.NET Core 1.0 and higher
.NET Framework 4.5 and higher




Here's an extension method (because everyone loves extension methods), with Lazlow's answer as the basis (chose Lazlow because it's easy to read).

It works the same as the regular ToString() method in DateTime except that if the format contains d or dd , the suffix will be added automatically.

 /// <summary> /// Return a DateTime string with suffix eg "st", "nd", "rd", "th" /// So a format "dd-MMM-yyyy" could return "16th-Jan-2014" /// </summary> public static string ToStringWithSuffix(this DateTime dateTime, string format, string suffixPlaceHolder = "$") { if(format.LastIndexOf("d", StringComparison.Ordinal) == -1 || format.Count(x => x == 'd') > 2) { return dateTime.ToString(format); } string suffix; switch(dateTime.Day) { case 1: case 21: case 31: suffix = "st"; break; case 2: case 22: suffix = "nd"; break; case 3: case 23: suffix = "rd"; break; default: suffix = "th"; break; } var formatWithSuffix = format.Insert(format.LastIndexOf("d", StringComparison.InvariantCultureIgnoreCase) + 1, suffixPlaceHolder); var date = dateTime.ToString(formatWithSuffix); return date.Replace(suffixPlaceHolder, suffix); } 
+5
Feb 21 '14 at 6:12
source share

Taking @Lazlow's answer to the complete solution, the following method is fully reused using an example:

 internal static string HumanisedDate(this DateTime date) { string ordinal; switch (date.Day) { case 1: case 21: case 31: ordinal = "st"; break; case 2: case 22: ordinal = "nd"; break; case 3: case 23: ordinal = "rd"; break; default: ordinal = "th"; break; } return string.Format("{0:dddd dd}{1} {0:MMMM yyyy}", date, ordinal); } 

To use this, you simply call it a DateTime object;

 var myDate = DateTime.Now(); var myDateString = myDate.HumanisedFormat() 

What will give you:

Friday June 17, 2016

+5
Jun 17 '16 at 12:19
source share

I believe this is a good solution, covering numbers like the 111th, etc .:

 private string daySuffix(int day) { if (day > 0) { if (day % 10 == 1 && day % 100 != 11) return "st"; else if (day % 10 == 2 && day % 100 != 12) return "nd"; else if (day % 10 == 3 && day % 100 != 13) return "rd"; else return "th"; } else return string.Empty; } 
+2
Jan 11 '13 at 11:44
source share

Cheap and cheerful VB solution:

 litDate.Text = DatePart("dd", Now) & GetDateSuffix(DatePart("dd", Now)) Function GetDateSuffix(ByVal dateIn As Integer) As String '// returns formatted date suffix Dim dateSuffix As String = "" Select Case dateIn Case 1, 21, 31 dateSuffix = "st" Case 2, 22 dateSuffix = "nd" Case 3, 23 dateSuffix = "rd" Case Else dateSuffix = "th" End Select Return dateSuffix End Function 
0
Mar 14 '13 at 13:59 on
source share

I did it like this, it touches on some of the problems given in other examples.

  public static string TwoLetterSuffix(this DateTime @this) { var dayMod10 = @this.Day % 10; if (dayMod10 > 3 || dayMod10 == 0 || (@this.Day >= 10 && @this.Day <= 19)) { return "th"; } else if(dayMod10 == 1) { return "st"; } else if (dayMod10 == 2) { return "nd"; } else { return "rd"; } } 
0
Mar 22 '13 at 11:13
source share

What is my final decision for using the answers below

  DateTime dt = DateTime.Now; string d2d = dt.ToString("dd").Substring(1); string suffix = (dt.Day == 11 || dt.Day == 12 || dt.Day == 13) ? "th" : (d2d == "1") ? "st" : (d2d == "2") ? "nd" : (d2d == "3") ? "rd" : "th"; Date.Text = DateTime.Today.ToString("dddd d") + suffix + " " + DateTime.Today.ToString("MMMM") + DateTime.Today.ToString(" yyyy"); 
0
Nov 11 '13 at
source share

public static String SuffixDate (date of date) {string serial number;

  switch (date.Day) { case 1: case 21: case 31: ordinal = "st"; break; case 2: case 22: ordinal = "nd"; break; case 3: case 23: ordinal = "rd"; break; default: ordinal = "th"; break; } if (date.Day < 10) return string.Format("{0:d}{2} {1:MMMM yyyy}", date.Day, date, ordinal); else return string.Format("{0:dd}{1} {0:MMMM yyyy}", date, ordinal); } 
0
Mar 21 '17 at 0:34
source share

Get the date suffix. (Static function)

 public static string GetSuffix(this string day) { string suffix = "th"; if (int.Parse(day) < 11 || int.Parse(day) > 20) { day = day.ToCharArray()[day.ToCharArray().Length - 1].ToString(); switch (day) { case "1": suffix = "st"; break; case "2": suffix = "nd"; break; case "3": suffix = "rd"; break; } } return suffix; } 

Link: https://www.aspsnippets.com/Articles/Display-st-nd-rd-and-th-suffix-after-day-numbers-in-Formatted-Dates-using-C-and-VBNet.aspx

0
Feb 13 '18 at 19:43
source share

there is no reference to a culture in the MSDN documentation that could convert 17 to 17th. so you have to do it manually using code. Close one ... you can create a function that does this.

 public string CustomToString(this DateTime date) { string dateAsString = string.empty; <here wright your code to convert 17 to 17th> return dateAsString; } 
-2
Jan 12 '10 at 17:22
source share

Another option using the last character of the string:

 public static string getDayWithSuffix(int day) { string d = day.ToString(); if (day < 11 || day > 13) { if (d.EndsWith("1")) { d += "st"; } else if (d.EndsWith("2")) { d += "nd"; } else if (d.EndsWith("3")) { d += "rd"; } else { d += "th"; } else { d += "th"; } return d; } 
-2
Oct 11
source share



All Articles