Timezone offset in javascript display

I am running into what seems to be a common problem of using a javascript datepicker to display and allowing users to select dates from a calendar of available charts to reserve numbers.

Browser timezone conversion means that these dates are always off with some swing, so often when displaying a reserved date for the user, the date will show "tomorrow" to the viewer when the server (timezone local to an asset or a room stored in the DB) shows them as today".

I would like the user's browser to ignore the javascript date conversion and just use the actual time that is passed in the database.

However, this even happens with a very simple example:

var date = '2013-02-05';

var newdate = new Date(date);

console.log(newdate); // Mon Feb 04 2013 16:00:00 GMT-0800 (PST) 

      

It looks like the variable is being date

used by the browser as GMT, and when I create a javascript date object it converts that GMT time to my local time.

Your best bet is to use the GMT data in the database in this case and set the local site local offset as a variable in javascript, which can then be used to offset the dates displayed to the end user and again the date offsets received from the end user to insert into database?

This is confusing as there are so many potential bugs - the PHP locale, the mysql locale, or the browser language can affect it and mess up the final date. Any advice on ensuring a consistent date value is appreciated!

+3


source to share


1 answer


Good question, time zone handling is a mess. Fortunately, Javascript has UTC variants of most of its methods. See http://en.wikipedia.org/wiki/Coordinated_Universal_Time

I think it's best to use UTC dates everywhere (except UI, maybe). Make sure the server uses and stores UTC dates and also uses UTC JavaScript methods everywhere. This is the first and most important step, so you know the dates are agreed.

How you represent dates in the UI is much less straightforward and depends on the target audience, the nature of the application, etc. I think there is more discussion topic and kind of irrefutable in Stack Overflow (there are other forums for subjective pondering).



I would say don't trust the browser or any geolocation to autoplay time; it should be a custom user or perhaps a specific group / project / install option. Some software is mostly used within the same time zone, and trying to automatically convert time zones can be confusing and annoying for users if they are used to communicate at a certain “standard” time. Should a user see times in different time zones if he or she is traveling? Sometimes it makes sense, sometimes it doesn't, but at least make sure the user knows what kind of logic you are following, both when reading and when entering time.

I am developing project management software where it is important that these things are handled unambiguously by the bot application and users (!). My approach is to always use UTC for display and input. Each date also clearly shows that it is UTC. Dealing with different time zones will be quick and I figured it was best not to. I have some helpers in some parts of the UI that display the same information in the local timezone of users, etc. In small print. There is a general project setup to "hide all timezone related stuff and force x timezone" which can be used in small projects that are known to never cross timezone boundaries.there is an agreement on the use of a certain zone or simply given and it is better not to bother users with such complexity.

EDIT: I must add that as an example of how hairy it can get, sometimes time is only time. In some contexts, an event at 3:00 pm may mean that it occurs at 3:00 pm in different locations, in their respective local time zones. Um ....

+1


source







All Articles