Monday, April 11, 2011

Does this custom DateTime format break in other cultures?

When displaying my DateTime values, I want them to be formatted like so: "February 9, 2009 7:00 AM"

I am using the following code, but am concerned that the date may display incorrectly in other cultures. Can anyone tell me if this is the case or not, and if it is how can I fix it?

Date.ToString("MMMM dd, yyyy hh:mm tt");

Thanks!

From stackoverflow
  • Why not simply use the user's long date/time format(s)?

  • the code

            DateTime now=DateTime.Now;
            foreach(CultureInfo inf in CultureInfo.GetCultures( CultureTypes.InstalledWin32Cultures))
            {
                Console.WriteLine(now.ToString("MMMM dd, yyyy hh:mm tt", inf.DateTimeFormat));
            }
    

    return
    Tlhakole 09, 2009 04:21 AM
    guovvamanu 09, 2009 04:21
    goevten 09, 2009 04:21
    veljaca 09, 2009 04:21
    kuovamaanu 09, 2009 04:21
    Hatun puquy 09, 2009 04:21 a.m.
    guovvamanu 09, 2009 04:21
    ta?lvvmannu 09, 2009 04:21
    Chwefror 09, 2009 04:21 a.m.
    februar 09, 2009 04:21
    guovvamano 09, 2009 04:21
    Hui-tanguru 09, 2009 04:21 a.m.
    Hatun puquy 09, 2009 04:21
    februar 09, 2009 04:21

    i mean, this is showing for each culture in his code

    epochwolf : You're up late :)
  • I am using the following code, but am concerned that the date may display incorrectly in other cultures.

    What exactly is ‘correct’? Are you a normal client-side app? If so, you'll get localised month names. Is that ‘incorrect’? Depends what you're looking for.

    If you do want locale-friendly times in a client-side app then using the default long date format will give you the smoothest results. In my locale I wouldn't want to see 12-hour clock or the month before the day, though I'd surely understand it.

    If you're a server-side app you don't want to use any default-localised anything, because the locale will be your server's rather than your users', and you'll have a weird portability problem. In this case you'll have to either allow the user to pick out their own locale, or use your own date formatting functions with built-in English month names.

    (Or, to avoid the problem, no month names. In which case ISO-8601 format “YYYY-mm-dd HH:MM:SS” would be the default choice.)

  • I prefer the ISO 8601 format (and yes without the "T") : you don't have to juggle among different representations depending on who is reading your data. Less code, less bugs.

    I keep finding documents originally written in english that get "translated" but with dates that stay "as is" and so become completely wrong.

    As an added bonus, ISO formatted dates sort just like ordinary strings !

0 comments:

Post a Comment