How to work better with time zones (timezones) in web applications?


Actually, a pretty hackneyed question, but still wanted to ask, how do you work with time zones user?
For myself until can choose from two scenarios:
1. In the database to store the timezone of the user indicated in the profile, then on its basis to produce manipulations with the times.
2. Timezone (timezoneOffset - does not matter) to determine the client every time you load your web application, then write in cookie and pass with each request to the server.
* in the DB all the times and dates are stored in UTC.
Or is there some more user-friendly and developer methods?
Thank you.
September 26th 19 at 06:54
3 answers
September 26th 19 at 06:56
And that is another way have a right to life.
The 2nd is probably easier if the user can actively change the time zone.
At the same time, the 1st in my opinion more stable and clearer to the user.
September 26th 19 at 06:58
Why do you even know her? Give timestamp in UTC format with timezone in a js client.
September 26th 19 at 07:00
In the end I chose the 2nd option, i.e. timezone to store in cookies.
Below we provide the implementation for ASP.Net Web Api (server) + JQuery (client)

The client (used library jquery.cookie):
setTimezoneCookie function() {
 timezone_cookie var = "tz";
 // Invert the offset (see the documentation for getTimezoneOffset)
 var timezone_offset = (new Date().getTimezoneOffset())*(-1);

 //If not - create
 if (!$.cookie(timezone_cookie)) {
 // check support cookies browser
 var isCookiesEnabled = 'isCookiesEnabled';
 $.cookie(isCookiesEnabled, true);
 if ($.cookie(isCookiesEnabled)) {
 // remove test cookie
 $.cookie(isCookiesEnabled, null);
 // writable timezone
 $.cookie(timezone_cookie, timezone_offset);
 else {
 // If a cookie with the timezone already there, but different then the new value
 storedOffset var = parseInt($.cookie(timezone_cookie));
 var currentOffset = timezone_offset;
 if (storedOffset !== currentOffset) {
 $.cookie(timezone_cookie, timezone_offset);

Create an HttpMessageHandler to intercept cookies:
public class TimezoneHandler : DelegatingHandler
 static public string TimezoneToken = "tz";

 async protected override Task<httpresponsemessage> SendAsync(
 HttpRequestMessage request, CancellationToken cancellationToken)
 // Initially offset from UTC zero
 int timezoneOffset = 0;

 // Get the value of the time zone of the cookies
 var cookie = request.Headers.GetCookies(TimezoneToken).FirstOrDefault();
 if (cookie != null)
 timezoneOffset = Int32.Parse(cookie[TimezoneToken].Value);
 catch (FormatException)
 // Error in the format of offset - set to zero
 timezoneOffset = 0;

 // Save a temporary shift in the properties of the HTTP request
 request.Properties[TimezoneToken] = timezoneOffset;

 // Continue the execution of the HTTP request
 HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

 return response;

Register the handler in WebApiConfig.cs:
public static void Register(HttpConfiguration config)
 config.MessageHandlers.Add(new TimezoneHandler());

Use in code:
int? tzOffset = Request.Properties[TimezoneHandler.TimezoneToken] as int?;

Find more questions by tags Work with date / time