locked
Changed CultureInfo... now got dates problem RRS feed

  • Question

  • User-1948529860 posted
    Hi,
    
    
    I have just put some code in to allow the culture of my website to be changed by the user...
    ...but now valid dates are causing exceptions to be thrown.
    
    
    
    
    The problem is my site isn't enitrely made using .NET and it has a uniPaaS backend.
    
    
    Basically, I need to make the site be DD/MM/YYYY, even when I change the CultureInfo
    to set the rest of the language/culture options.
    
    
    
    
    Any way this can be set in web.config, say?
    
    
    Thanks
    G
    Tuesday, August 16, 2011 7:12 AM

All replies

  • User1266497125 posted

    In web.config you can set globalization for the entire web site. What kind of problems are you getting now? Basically if you keep value of a date in a DateTime object then the globalization comes into play only when you use myDateTime.ToString() (or some other string method as part of DateTIme - are you passing date/time as a string to your object?). At that point the globalization will kick in and your date will be formatted according to that globalization, otherwise DateTime doesn't care much about formatting. However, if you are passing date as a string to that custom component then use ToString() with formatting to suit your needs. For example:

    DateTime dtMyDate = new DateTime(2011, 8, 16);
    
    Console.WriteLine("Date (formatted by current culture): {0}", dtMyDate.ToString());
    Console.WriteLine("Date (my format): {0}", dtMyDate.ToString("dd/MM/yyyy"));

    If your current culture is set to US the date will be formatted as month/day/year (8/16/2011), if it is UK it will be day/month/year (16/8/2011), German globalization is day.month.year (16.8.2011). The second output line does not care about globalization and prints the date according to the format that we specified (dd/MM/yyyy).

    Regards,

    Huske

    Tuesday, August 16, 2011 7:22 AM
  • User-1948529860 posted

    Thanks for your post.

    Sadly, right now, I already have a site with a million lines of code dealing with dates as strings.

    Due to the current culture settings (en-GB), the dates are all expected in DD/MM/YYYY format. This is the right format to communicate with uniPaaS.

    It would be a MAMMOTH task to go through the existing code and change every line of code that changes a date to a string, outputs a date or changes a string to a date.

    Is there a way to allow the culture to be set to something other than en-GB but to then OVERRIDE that when it comes to dates with a DD/MM/YYYY format across the site?

    So...

    1) Allow Culture and CultureUI to be set

    2) Keep ALL dates across the site as DD/MM/YYYY

    3) Change MINIMUM amounts of code.

    Can I satisfy these 3 requirements at once?

    Cheers

    Tuesday, August 16, 2011 11:15 AM