C#, datetime formatting, mont name, cultureinfo
Asked Answered
T

4

7

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?

Trapper answered 12/3, 2017 at 7:10 Comment(5)
There are predefined standard formats, see: msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspxLawabiding
Please show minimal reproducible example - there is no way to know how you got culture wrong.Baryta
@EvanTrimboli it is not going to help OP since they can't get reasonable cultureInfo to start with based on the output.Baryta
You are asking for two opposite things here: you want to use the culture format BUT you don't like the culture format and say "I want it to be like this, but I want it to feel denmark-like"... It is quite non-sense.Mettlesome
Thank you. Xanatos, I have probably not been accurate enough in my description. Selecting cultureinfo for Denmark, should show the date as standard danish way of showing a date when including the name of the month. Selecting cultureinfor for England should show it the english way. Again, including the name of the month. And the same for all other countries.Trapper
S
12

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"));
Sobranje answered 12/3, 2017 at 7:37 Comment(2)
Thank you, I believe this is as close as I can get. Too bad about the name of the day not appearing in all cultures. If it had always been missing, it would be a simple task including it.Trapper
No it is not too bad that the name of the day is not appearing: That is how that culture wants it so we cannot decide for them.Sobranje
T
1

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!

Trapper answered 12/3, 2017 at 8:35 Comment(0)
E
0

This non-customized code snippet gets you close.

Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("da-DK");
DateTime dt = DateTime.Now;
Console.WriteLine($"{dt.ToLongDateString()} {dt.ToLongTimeString()}");
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine($"{dt.ToLongDateString()} {dt.ToLongTimeString()}");
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("ar-AE");
Console.WriteLine($"{dt.ToLongDateString()} {dt.ToLongTimeString()}");
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN");
Console.WriteLine($"{dt.ToLongDateString()} {dt.ToLongTimeString()}");

Outputs:

12. marts 2017 20:36:38
Sunday, March 12, 2017 8:36:38 PM
12 مارس, 2017 08:36:38 م
2017年3月12日 20:36:38

However, to get customized output for locales, e.g. the day to appear in Danish, you'll have to code special cases.

Erymanthus answered 12/3, 2017 at 7:35 Comment(1)
Thank you, I believe this is as close as I can get. Too bad about the name of the day not appearing in all cultures. If it had always been missing, it would be a simple task including it.Trapper
M
0

There are something like 100 cultures that don't include week day in the FullDateTimePattern. You can see them with a short program like this:

var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

foreach (var culture in cultures.OrderBy(x => x.EnglishName))
{
    if (!culture.DateTimeFormat.FullDateTimePattern.Contains("dddd"))
    {
        Console.WriteLine("{0} ({1}): {2}", culture.EnglishName, culture.Name, culture.DateTimeFormat.FullDateTimePattern);
    }
}

Many european or european-descendant cultures don't include it (I see Croatian (Croatia) (hr-HR), Danish (Denmark) (da-DK), English (United Kingdom) (en-GB), French (Canada) (fr-CA), Swedish (Sweden) (sv-SE)), plus I see Japanese, Russian, Chinese... The world is a big place, and in many places the week day isn't considered to be part of the date. You have two choices: accept it, or for any cultures you don't like the date pattern, create your FullDateTimePattern that contain whatever you want.

For example you could:

public static CultureInfo DanishCultureInfo = InitCultureInfoWithWeek("da-DK");

private static CultureInfo InitCultureInfoWithWeek(string name)
{
    var ci = CultureInfo.GetCultureInfo(name);
    ci = (CultureInfo)ci.Clone();
    ci.DateTimeFormat.FullDateTimePattern = "dddd " + ci.DateTimeFormat.FullDateTimePattern;
    ci = CultureInfo.ReadOnly(ci);
    return ci;
}

public static CultureInfo GetCultureForPrinting(string name)
{
    var ci = CultureInfo.GetCultureInfo(name);

    if (string.Equals(name, "da-DK", StringComparison.InvariantCultureIgnoreCase))
    {
        return DanishCultureInfo;
    }
    else
    {
        return CultureInfo.GetCultureInfo(name);
    }
}

And then when you want to use a culture to print something, instead of using CultureInfo.GetCultureInfo() you use GetCultureForPrinting()

Mettlesome answered 12/3, 2017 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.