locked
Globalization and localization of DateTime. RRS feed

  • Question

  • Hi,
       I do know that given a UTC datetime you can specify to convert it to required time zone and you can feed in culture info to get it localised.

    Problem : As per my current requirement I was to show date time format in form of "Nov 20, 2008 1:30 P.M EST". But when i use DateTime.ToString("F", culture); Output is "Wednesday, Novermber 20 2008 1:30 PM EST".

    To avoid this I tried to specify the format in ToString(). Code is DateTime.ToString("MMM dd YYYY HH mm ss", culture);

    But output is always shows 24 hour clock time. Instead I want it to be based on the culture provided. Any help over this would be great favour.

    Thanks and regards,
    Giridhar.
    Thursday, February 19, 2009 1:19 PM

Answers

  • Why do you store the culture information in session?  Why not set the current culture of the thread and call it a day?  ASP.NET has a separate thread per session, so this shouldn't be a problem.

    As for when it gives one thing and when it gives another, you may want to check the current culture of the browser.  If the browser has an incorrect culture setting, then the page will have an incorrect culture setting. 

    BTW, for future reference, ASP.NET questions should be asked at the ASP.NET Forums at http://forums.asp.net/.
    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Harry Zhu Tuesday, February 24, 2009 1:57 AM
    • Marked as answer by Harry Zhu Thursday, February 26, 2009 2:36 AM
    Thursday, February 19, 2009 3:06 PM
  • I'm afraid it's a bit more complicated than that.  The "F" format differs from culture to culture.  In order to get this proper, you'll have to reset the format of the CultureInfo.  Now, in the example below, I've given two possibilities.  One is commented out.  Experiment with both and see which works for you.  I simply took the route with this question of creating a test for myself to try to get what you want.  I ran through all the specific cultures in the system to see if this would work for all of them.  It does.  

    Now, you have to keep in mind that the standard formatting strings are really just "Microsoft-designed custom formatting strings". That is, when you put in "F", it's going to check the format of the current culture's NumberFormatInfo.FullDateTimePattern, which, if you were to look at, would look just like one of your custom formatting strings.  Because of this, in order to change the FullDateTimePattern to what you want, you'll need to manually parse the FullDateTimePattern of the culture's NumberFormatInfo, and feed it back into the culture again.  This is what I do below.  After that, you have two choices.  You can either pass that culture in directly to the ToString method, or you can set it as the current culture's culture. 

    Hopefully this should give you a good start.

    Form form1 = new Form();

    form1.Height = 700;

    form1.Width = 700;

    form1.StartPosition = FormStartPosition.CenterScreen;

     

    RichTextBox box = new RichTextBox();

    box.Dock = DockStyle.Fill;

    box.Font = new Font("Consolas", 8F);

    form1.Controls.Add(box);

     

    foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))

    {

        string name = culture.DisplayName.PadRight(55);

     

        // You'll have to do some altering of the pattern to get this effect.

        string format = culture.DateTimeFormat.FullDateTimePattern;

        int first = format.IndexOf("M");

        int last = format.LastIndexOf("M");

        format = format.Substring(0, first) + "MMM" + format.Substring(last + 1);

     

        // for formatting only

        // culture.DateTimeFormat.FullDateTimePattern = format;

        // string date = DateTime.Now.ToString("F");

       

        // for formatting and language

        string date = DateTime.Now.ToString(format, culture);

     

        string newline = Environment.NewLine;

        string text = string.Format("{0}{1}{2}", name, date, newline);

        box.AppendText(text);

    }

     

    form1.ShowDialog(); 


    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Harry Zhu Tuesday, February 24, 2009 1:57 AM
    • Marked as answer by Harry Zhu Thursday, February 26, 2009 2:36 AM
    Friday, February 20, 2009 12:09 PM

All replies

  • 24 hours format is because of 'HH'. You have to use 'hh', but for displaying AM/PM add 'tt'. Finally, it must be:

    "MMM dd, yyyy hh:mm tt"

    Hope it helps.


    void
    Thursday, February 19, 2009 1:43 PM
  • Thanks for your reply. But, now it is always 12 hour clock. Can we have it based on the culture of the user.
    Thursday, February 19, 2009 2:16 PM
  • To Add further if you give DateTime.ToString("F", culture); The output is some times 24 hour clock and some times it is 12 hour clock(based on culture).
    Thursday, February 19, 2009 2:18 PM
  • What's your context?  ASP.NET? Forms?  Where you get your CultureInfo from depends on this.  In WinForms, you don't even need to set the culture.  In ASP.NET, it should pick up the culture of the user automatically as well, but if you're using it in, say, the background of a service or something, it might pick up the culture of the machine running the service, as opposed to the UI culture.  We need more information about the architecture you're working in in order to give a proper answer.
    David Morton - http://blog.davemorton.net/
    Thursday, February 19, 2009 2:50 PM
  • Hi,
      It's ASP.net. In our current application we store culture information of user in session. So we need to give it explicitly. Will his help you in figuring out the issue. Please do let me know if you need further clarification.
    Thursday, February 19, 2009 2:55 PM
  • Why do you store the culture information in session?  Why not set the current culture of the thread and call it a day?  ASP.NET has a separate thread per session, so this shouldn't be a problem.

    As for when it gives one thing and when it gives another, you may want to check the current culture of the browser.  If the browser has an incorrect culture setting, then the page will have an incorrect culture setting. 

    BTW, for future reference, ASP.NET questions should be asked at the ASP.NET Forums at http://forums.asp.net/.
    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Harry Zhu Tuesday, February 24, 2009 1:57 AM
    • Marked as answer by Harry Zhu Thursday, February 26, 2009 2:36 AM
    Thursday, February 19, 2009 3:06 PM
  • We do have the option of giving in the culture info to th DateTime.ToString(), I just want to know will it honour it when you specify the culture name aswellas format... Which is going to take priority? Can we set priority?
    Thursday, February 19, 2009 3:15 PM
  • It does honor it with format "F".  The "F" is the current "FullDateTimePattern" from the current culture or the culture passed into the ToString method. 

    The following link gives more info on how certain standard DateTime format strings are interpreted at runtime:

    Standard Date and Time Format Strings
    David Morton - http://blog.davemorton.net/
    Thursday, February 19, 2009 4:09 PM
  •  Thanks for your reply, but I want to know if it honours with custom format provided DateTime.ToString("MMM dd YYYY HH mm ss", culture);
    Friday, February 20, 2009 6:01 AM
  • Let me putforth  my question simple terms. I want DateTime.ToString() to give exactly sample output as DateTime.ToString("F", culture) except that month should be in short form(i.e insteda of november it should be nov....)
    Friday, February 20, 2009 6:05 AM
  • I'm afraid it's a bit more complicated than that.  The "F" format differs from culture to culture.  In order to get this proper, you'll have to reset the format of the CultureInfo.  Now, in the example below, I've given two possibilities.  One is commented out.  Experiment with both and see which works for you.  I simply took the route with this question of creating a test for myself to try to get what you want.  I ran through all the specific cultures in the system to see if this would work for all of them.  It does.  

    Now, you have to keep in mind that the standard formatting strings are really just "Microsoft-designed custom formatting strings". That is, when you put in "F", it's going to check the format of the current culture's NumberFormatInfo.FullDateTimePattern, which, if you were to look at, would look just like one of your custom formatting strings.  Because of this, in order to change the FullDateTimePattern to what you want, you'll need to manually parse the FullDateTimePattern of the culture's NumberFormatInfo, and feed it back into the culture again.  This is what I do below.  After that, you have two choices.  You can either pass that culture in directly to the ToString method, or you can set it as the current culture's culture. 

    Hopefully this should give you a good start.

    Form form1 = new Form();

    form1.Height = 700;

    form1.Width = 700;

    form1.StartPosition = FormStartPosition.CenterScreen;

     

    RichTextBox box = new RichTextBox();

    box.Dock = DockStyle.Fill;

    box.Font = new Font("Consolas", 8F);

    form1.Controls.Add(box);

     

    foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))

    {

        string name = culture.DisplayName.PadRight(55);

     

        // You'll have to do some altering of the pattern to get this effect.

        string format = culture.DateTimeFormat.FullDateTimePattern;

        int first = format.IndexOf("M");

        int last = format.LastIndexOf("M");

        format = format.Substring(0, first) + "MMM" + format.Substring(last + 1);

     

        // for formatting only

        // culture.DateTimeFormat.FullDateTimePattern = format;

        // string date = DateTime.Now.ToString("F");

       

        // for formatting and language

        string date = DateTime.Now.ToString(format, culture);

     

        string newline = Environment.NewLine;

        string text = string.Format("{0}{1}{2}", name, date, newline);

        box.AppendText(text);

    }

     

    form1.ShowDialog(); 


    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Harry Zhu Tuesday, February 24, 2009 1:57 AM
    • Marked as answer by Harry Zhu Thursday, February 26, 2009 2:36 AM
    Friday, February 20, 2009 12:09 PM