Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 431
Mads Aggerholm Avatar asked Mar 12 '17 07:03

Mads Aggerholm


2 Answers

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"));
like image 178
CodingYoshi Avatar answered Sep 30 '22 00:09

CodingYoshi


Thank you all! Your input lead me to this solution:

public string fn_date(DateTime par_datetime, CultureInfo par_cultureinfo)
        {
            string st_day = par_datetime.ToString("dddd", par_cultureinfo);
            string st_date = par_datetime.ToString("T", par_cultureinfo);
            if (st_date.ToUpper().IndexOf(st_day.ToUpper()) == -1) st_date = st_day + ", " + st_date;

            return st_date;
        }

I understand that the name of the day is not 'standard', but the reason I need this is because I'm making an app for old and/or demented people who has difficulties keeping track of what day and month it is. Originally I made this for my father, who had this exact problem. I got hold of an old laptop, and had it write the date and time clearly with big letters at the bottom of the screen. The rest of the screen showed various pictures of his grandchildren. It was a big help.

So I wanted to improve it and make it international, so everyone could download it from my site and use it if they had the need.

Thanks again!

like image 33
Mads Aggerholm Avatar answered Sep 30 '22 01:09

Mads Aggerholm