0

I want the user to enter an appointment date and time and send him a reminder an hour before it.

On the server I want to store the date-time in UTC, but on the client the user will enter it in his local time. Whats the best way to deal with this conversion? Is there a plugin that natively supports this?

Saurav Shah
  • 661
  • 3
  • 8
  • 16

2 Answers2

2

Yes, there's a plugin which detects the timezone according to the system-information (jsTimezoneDetect). Actually a date-object itself stores the information (but it's kinda wierdo!).

There's an interesting article and an interesting answer here on SO, which is worth a read!

Don’t do any date-time computations in javascript. You may guess the browsers time zone to define it, but the definition itself and all date-time computations must be made in a time-zone aware system.

As you will read this article, you'll get a deeper understanding of the problem "date/dst/utc/timezones in javascript" - therefore it would be interesting if you tell us more about your server-side:

  • Which framework do you use on your server-side?
  • Do you have any logic for represeting accounts with some settings (eg custom timezone-settings)?

The "correct" approach (which is fail-safe if you have the backend behind it) will be: let the user work with a datetime - in the browser-scope it's more or less "undefined" (no need to find the timezone, offset, or anything alike!). Now you provide this "undefined" datetime information to the server (eg sending the unix-timestamp with a hiddenfield to the server), where you have the information which actual timezone the user is assigned to. So you can now translate this "undefined" datetime-information to a time-zone aware datetime-object on the server-side and assign the information of your account-scope to it (now the "undefined" datetime-object has a local time-zone - eg "Pacific Daylight Time"). Now you can easily do any conversion you want.
The only downside of this approach is, that you will have 2 scopes of defining a timezone (OS of the user and your webapp) - but anything else is fiddling and not fail-safe!

Community
  • 1
  • 1
  • Interesting pointers. So if I understand you correctly, I can have a hidden field sent to the server which calculates the correct milliseconds from epoch based on user input of date and time. One way would be use the jsTimezoneDetect file to add offset. If I do new Date(year, month, day, hours, minutes, seconds, milliseconds) does this take care of the offest automatically? – Saurav Shah May 07 '12 at 05:32
  • Actually ... yes! That's how we did it: Let the user eg say '2012-01-01 12:30:00' (ok ... correct ... not a string, rather a date-object). Now we convert this to a unix timestamp (this conversion depends on the OS settings) and send it to the server where we convert it (as we are on ASP.NET) to a `System.DateTime` and set it to "local" with the timeZone-information we have from the user her-/himself. Now we can convert to real UTC and do any conversion we want. If we are in the need to show some dateTime (which is always stored in UTC), we convert to the local value of the displaying user. –  May 07 '12 at 05:41
0

It may be an idea to send a timestamp to your server, using the getTimezoneOffset method to calculate the UTC date/time client side. To convert it back client side you'd send the timestamp, create a dummy date client side to determine the currend timezone offset (the user may have moved to another timezone!) and create a client side date from that information. Client side you'd do something like:

//[client side send]
var d = new Date(), //now
    offset = d.getTimezoneOffset()
    UTCTimeStamp = d.getTime()+(60000*offset); // send to server

//[client side receive]
var offset = 60000*new Date().getTimezoneOffset(),
    localDate = new Date(timestamp + (offset < 0 ? Math.abs(offset) : -offset));

Click here to do a simple test using this idea.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • this has a main issue: if you are on a dst-timezone you'll get different timezone offsets for different dates ... eg 2012-01-01 with +1, whereas 2012-05-07 with +2. now ... "+2" can be a timezone with or without dst ... –  May 07 '12 at 05:23
  • btw - the result of `.getTimezoneOffset()` is in minutes –  May 07 '12 at 05:26
  • @Andreas: try the link from my answer. The client sends a UTC-timestamp to my server, the timestamp is returned by the server and processed client side to a date/time in the clients' timezone. – KooiInc May 07 '12 at 08:02