Tech Off Thread

13 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

ISO 8601 date formats in ASP.NET

Back to Forum: Tech Off
  • User profile image
    W3bbo

    'lo

    Does anyone know how to get Web.config's <globalization /> element (or whatever is appropriate) to ensure that an ASP.NET application renders (and works with) dates using the ISO 8601 date format?

    Whilst I could do .ToString(FormatString), that's too much effort for all the times dates are rendered in the application.

    I hear references about setting <globalization culture="en" /> then overriding the date formatter, but I get exceptions about "en" being a 'neutral culture'.

    Any ideas?

  • User profile image
    TommyCarlier

    You'll probably have to be more specific than 'en'. I don't know about this specific situation, but I've had that kind of exceptions elsewhere, and it means that 'en' is not a specific culture, but a neutral culture. You'll have to use a specific culture like 'en-US' or 'en-GB'.

  • User profile image
    W3bbo

    TommyCarlier wrote:
    You'll probably have to be more specific than 'en'. I don't know about this specific situation, but I've had that kind of exceptions elsewhere, and it means that 'en' is not a specific culture, but a neutral culture. You'll have to use a specific culture like 'en-US' or 'en-GB'.


    Yes, but I want to have en-US as the culture, but with ISO 8601 date formats.

  • User profile image
    blowdart

    W3bbo wrote:
    TommyCarlier wrote:You'll probably have to be more specific than 'en'. I don't know about this specific situation, but I've had that kind of exceptions elsewhere, and it means that 'en' is not a specific culture, but a neutral culture. You'll have to use a specific culture like 'en-US' or 'en-GB'.


    Yes, but I want to have en-US as the culture, but with ISO 8601 date formats.


    You can't. Date formatting is always culture specific, unless you override via string.format, or change the culture. There's no way to pick and choose.

  • User profile image
    W3bbo

    blowdart wrote:
    You can't. Date formatting is always culture specific, unless you override via string.format, or change the culture. There's no way to pick and choose.


    Ooookay, so what culture has YYYY-MM-DD date/time formats then?

  • User profile image
    blowdart

    W3bbo wrote:
    blowdart wrote:You can't. Date formatting is always culture specific, unless you override via string.format, or change the culture. There's no way to pick and choose.


    Ooookay, so what culture has YYYY-MM-DD date/time formats then?


    Hmm. Pass. Go enumerate Smiley

  • User profile image
    Maurits

    W3bbo wrote:
    blowdart wrote: You can't. Date formatting is always culture specific, unless you override via string.format, or change the culture. There's no way to pick and choose.


    Ooookay, so what culture has YYYY-MM-DD date/time formats then?


    None, which is odd, because it's such a natural format.  Alphabetic and chronological sort orders coincide.

  • User profile image
    W3bbo

    Maurits wrote:
    W3bbo wrote:
    blowdart wrote: You can't. Date formatting is always culture specific, unless you override via string.format, or change the culture. There's no way to pick and choose.


    Ooookay, so what culture has YYYY-MM-DD date/time formats then?


    None, which is odd, because it's such a natural format.  Alphabetic and chronological sort orders coincide.


    So how do I get YYYY-MM-DD in my ASP.NET app then? Wink

  • User profile image
    blowdart

    W3bbo wrote:


    So how do I get YYYY-MM-DD in my ASP.NET app then? Wink


    string.Format or DateTime.ToString("yyyy-MM-dd");

    Now the second may be more suitable, you could set the format string in web.config, so you can change it later Smiley

  • User profile image
    W3bbo

    blowdart wrote:
    W3bbo wrote:

    So how do I get YYYY-MM-DD in my ASP.NET app then? Wink


    string.Format or DateTime.ToString("yyyy-MM-dd");

    Now the second may be more suitable, you could set the format string in web.config, so you can change it later Smiley


    Hold on, I've been looking at this MSDN article: (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemglobalizationdatetimeformatinfoclasstopic.htm)

    Aparently, I can change the current thread's culture's datetime format in Global.asax.cs during Application_Start()

    ...but I can't find any examples, and what I try to set it, I get this error

    Instance is read-only. - System.Globalization.DateTimeFormatInfo.set_LongDatePattern(String value) +77


    Here's the code I'm using:

    System.Globalization.DateTimeFormatInfo.CurrentInfo.LongDatePattern = "YYYY-MM-DD";

    EDIT:

    I also tried this, but it fails with the same error:

    System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd";

  • User profile image
    W3bbo

    Okay, I've got this now:

            // Set dates to ISO 8601 format
            private Globalization.CultureInfo CI;
            CI = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            CI.DateTimeFormat.LongDatePattern = "yyyy-MM-dd";
            
            Threading.Thread.CurrentThread.CurrentCulture = CI;
    


    Whilst that doesn't crash, it doesn't work either. Dates are still rendered using MM/DD/YYYY format.

  • User profile image
    blowdart

    Console.WriteLine("Current Date Format : {0}\n", System.Globalization.DateTimeFormatInfo.CurrentInfo.LongDatePattern);
    Console.WriteLine(DateTime.Now.ToLongDateString());

    CultureInfo myCustomCulture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
    myCustomCulture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd";

    Thread.CurrentThread.CurrentCulture = myCustomCulture;
    Console.WriteLine("Current Date Format : {0}\n", System.Globalization.DateTimeFormatInfo.CurrentInfo.LongDatePattern);           
    Console.WriteLine(DateTime.Now.ToLongDateString());

    Now for me that gives

    Current Date Format : dd MMMM yyyy

    23 October 2005
    Current Date Format : yyyy-MM-dd

    2005-10-23

  • User profile image
    W3bbo

    Nevermind, I've got it sorted now Smiley

    The rendered dates were using "FullDateTimePattern" rather than "LongDateTimePattern"

    So its fixed now, woo Smiley

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.