user1497574 user1497574 - 1 year ago 57
C# Question

C#, datetime formatting, mont name, cultureinfo

I'm currently working on a small program which I want to show the date and time clearly and according to cultureinfo, like this:
Sunday, march 3. 2017 7:46:57 AM

However, I want it to be possible to select any country, and get the date output according to their way of writing the date. In Denmark, as an example, I'd like to show:
S√łndag, 3. marts 2017 7:46:57 (<-- in the evening it would be '19:46:57')

Please note, that the '3' is on the other side of the month name.

I can get all the cultureinfo's using CultureInfo.GetCultures, no problem, but when I show the datetime I get 'Sunday 3/12/2017 7:46:57 AM' using MyDateTime.ToString(MyCultureInfo);

I know how to extract the month name, and I could manually construct a string where all these data is showing the way I want for cultures I know, but I can not for cultures I do not know. If somebody chooses 'Chinese' og 'Arabic', I wouldn't know what to show.

I was thinking if there is some way to get the full date with month name in the right place for any culture?

Answer Source

I do not think the format you are looking for is the default format for all cultures. The code below will display the date in the default Full format for the specified culture. F is for FullDateTimePattern.

var now = DateTime.Now;
var denmark = now.ToString("F", new CultureInfo("da-DK"));

According to that line of code the output is:

12. marts 2017 03:34:12

So my takeaway from that is: The default full datetime pattern in Denmark is as the above. If you want another format, then use the code below and tweak it as you need to but keep in mind people in Denmark may find this pattern odd since they may not be used to it:

var denmarkCustomized = now.ToString("dddd, MMMM dd, yyyy h:mm:ss tt"
            , new CultureInfo("da-DK"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download