Date date function in .NET.

Is there a built-in .NET date formatting function to return a date in the format:

April 3, 2010

This is the "rd" bit, which I can’t remember if there is a formatting option.

Greetings Rob.

+4
source share
3 answers

Do not think what is built in there. But it's pretty easy to copy / paste one of the blog post :

Protected Function FormatPostedTimeWithDayNumberSuffix(Optional ByVal format As String = "MMMM d{0}, yyyy") As String Dim dateTime As DateTime = Date.Parse(Entry.PostedTime) If Not Null.IsNull(dateTime) Then Dim formatTime As String = dateTime.ToString(format) Dim day As Integer = dateTime.Day Dim dayFormat As String = String.Empty Select Case day Case 1, 21, 31 dayFormat = "st" Case 2, 22 dayFormat = "nd" Case 3, 23 dayFormat = "rd" Case Else dayFormat = "th" End Select Return String.Format(formatTime, dayFormat) End If Return Entry.PostedTime End Function 
+4
source

I am afraid that you should do it yourself. In C #, it will be something like that:

 if (dt.Day == 1) strdate = dt.ToString("dd") + "st"; else if (dt.Day == 2) strdate = dt.ToString("dd") + "nd"; else if (dt.Day == 3) strdate = dt.ToString("dd") + "rd"; else if (dt.Day == 21) strdate = dt.ToString("dd") + "st"; else if (dt.Day == 22) strdate = dt.ToString("dd") + "nd"; else if (dt.Day == 23) strdate = dt.ToString("dd") + "rd"; else if (dt.Day == 31) strdate = dt.ToString("dd") + "st"; else strdate = dt.ToString("dd") + "th"; 
0
source

Thanks to everyone - I suspected a lot, but it was never a good idea to rely on memory. There is no built-in function / formatting for this requirement, so you need to minimize it yourself.

Greetings Rob.

0
source

Source: https://habr.com/ru/post/1305925/


All Articles