Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date / time conversion from user's local time to UTC on website

I'm currently adding an out of office like system to a website, where users will be able to mark their out of office dates and times such that they can provide another users's information to use as a backup while they are out.

The problem I have, is converting the users's local time to UTC. I've seen other posts that address this issue by supplying UTC to the user, and having the client (js) convert the time to and from local time. I do, however, have access to a propriety system I can use to convert the date server-side based on the user's time-zone preference.

My question is this: Should I use the server side conversion, which would allow a user's home local time to be supplied (say, their US time, regardless of where they are logged in), or should I use the client side conversion?

Does anyone have any experience with this? What are some of the less obvious advantages / disadvantages of each method?

like image 213
cwharris Avatar asked Apr 14 '11 17:04

cwharris


People also ask

How do you convert date to UTC format?

The Javascript date can be converted to UTC by using functions present in Javascript Date object. The toUTCString() method is used to convert a Date object into a string, according to universal time. The toGMTString() returns a string which represents the Date based on the GMT (UT) time zone.

How do you convert local time zone to UTC?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

How do I convert local TimeZone to date?

Use the Date() constructor to convert UTC to local time, e.g. new Date(utcDateStr) . Passing a date and time string in ISO 8601 format to the Date() constructor converts the UTC date and time to local time.

Is new date () UTC or local?

getTime() returns the number of milliseconds since 1970-01-01. If you create a new Date using this number, ex: new Date(Date. getTime()); it will be UTC, however when you display it (ex: through the chrome dev tools console) it will appear to be your local timezone.


1 Answers

Storing everything in UTC is a very good idea and I recommend that you stick to it.

There are few approaches you can use. I assume that you would have User's profiles. You may decide to give users an ability to pick the time zone they prefer and display everything using this information. As well as treat all user input appropriately (convert on server side, assuming the time zone is preferred one).

Another way to handle it, is to actually take User's time zone offset via Date.getTimeZoneOffset() and send it out to your server somehow (i.e. via hidden form field or Ajax). When you know that, showing everything in User's time would be piece of cake. In this case you might want to convert date/time on the client and send it out as UTC (or use hidden form field with time zone offset).

In both cases you handle database date conversion on server side. This is the best approach from my experience. I would like to point out, that apart from just converting time zones, you probably should think about displaying date/time in correct format (i.e. depending on AcceptLanguage header value).

like image 51
Paweł Dyda Avatar answered Sep 22 '22 14:09

Paweł Dyda