Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display localized date and time information to web users with ASP.NET
    primarykey
    data
    text
    <p>I have an ASP.NET application and a UTC time stamp on the server. I want to display the time stamp to the user <b>in the right time zone and using local date/time format</b>.</p> <p>E.g. <code>Jan 2, 2012 14:00:00 UTC</code> should show as <code>1/2/2012 9:00 AM</code> to a user in New York, United States (UTC -0500) and as <code>02/01/2012 14:00</code> to a user in London, UK. </p> <p>This seemingly simple task proved to be surprisingly difficult. <a href="http://msdn.microsoft.com/en-us/library/bb882561.aspx" rel="nofollow">MSDN has an article</a> with the same title, but it talks about parsing user input rather than displaying server-side data, so it does not fully apply.</p> <p>Time zone offset is easily determined on the client via JavaScript</p> <p><code>offset = new Date().getTimezoneOffset();</code>, </p> <p>but JavaScript provides very poor support for date/time formatting. All you get is <code>toLocaleString()</code> method, which results in ugly long string such as <code>Monday, January 02, 2012 9:00:00 AM</code>. There is no provision for shorter formats, so on the client we're stuck with good time zone info and bad date/time format capabilities.</p> <p>The situation is exactly opposite on the server. We can leverage Accept-Language HTTP header to get the user locale (<a href="http://www.w3.org/International/questions/qa-accept-lang-locales" rel="nofollow">not the best choice, but may be good enough</a>), and then tap into the .NET database of known locales, so our code goes along the lines of </p> <p><code>CultureInfo userCulture = new CultureInfo(Request.UserLanguages[0]);</code> plus some error handling.</p> <p>But then we are stuck on the time zone problem. Yes, one can get it via JavaScript and then pass back as a cookie or as postback data, but what if we need to display dates on the very first page of the application? One may argue that the first page is always the login page, but it is not so when user login info is persisted between sessions ("remember me" option). The solution to that could be to save time zone offset as part of user profile, but then it may easily become stale (getting on and off daylight savings time between sessions).</p> <p>Is there a comprehensive solution to this problem that performs well and does not require writing tons of code? I am very curious, please advise.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload