I have an ASP.NET application and a UTC time stamp on the server. I want to display the time stamp to the user in the right time zone and using local date/time format.
E.g. Jan 2, 2012 14:00:00 UTC
should show as 1/2/2012 9:00 AM
to a user in New York, United States (UTC -0500) and as 02/01/2012 14:00
to a user in London, UK.
This seemingly simple task proved to be surprisingly difficult. MSDN has an article with the same title, but it talks about parsing user input rather than displaying server-side data, so it does not fully apply.
Time zone offset is easily determined on the client via JavaScript
offset = new Date().getTimezoneOffset();
,
but JavaScript provides very poor support for date/time formatting. All you get is toLocaleString()
method, which results in ugly long string such as Monday, January 02, 2012 9:00:00 AM
. 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.
The situation is exactly opposite on the server. We can leverage Accept-Language HTTP header to get the user locale (not the best choice, but may be good enough), and then tap into the .NET database of known locales, so our code goes along the lines of
CultureInfo userCulture = new CultureInfo(Request.UserLanguages[0]);
plus some error handling.
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).
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.