Special order of date elements

I would like to show a month with letters and day for users. I could use DateTime.ToString("MMMM d"); but in some countries the correct format would be d MMMMM .

Is there a way to ensure that order is always right?

+4
source share
3 answers

No, there is no way. Just browse

 [Globalization.CultureInfo]::GetCultures([Globalization.CultureTypes]::AllCultures) | select @{l='Culture';e={$_.Name}},@{l='Pattern';e={$_.DateTimeFormat.LongDatePattern}} 

reveals many cultures where there are things around the name of the month and day, which should be included in some and excluded in some others.

+3
source

Is there a way to ensure that order is always right?

Well, the closest I can think of would be to get the correct β€œlong date format” (for example, CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern , and then delete any item that you don't need.

However, this will be a rather fragile approach, and I would recommend that you thoroughly test all cultures that are of particular interest to you. For example, you need to think of a culture where a long date pattern does not include "MMMM" ... or take en-US , for example, where a long date pattern uses dd instead of d . What would you like to do in such situations?

Then there is the whole genitive case against non-parent forms - I suspect that for any cultures where it matters, both the long date format and the "month" format that you use will use the same form ... but that's honest saying everything gets pretty dirty.

+2
source

You can use the ToString() overload, which accepts CultureInfo , and use the appropriate culture.

For instance:

 var american = new CultureInfo("en-us"); var dateString = new DateTime(2000,1,1).ToString("MMMM d", american); 

gives January 1st.

Additional information in this matter .

-1
source

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


All Articles