Javascript date object in different locales and timezone

I need to write a web application that shows the events of people in different language standards. I am almost done, but there are two problems with the date:

  • using a javascript date object, the date depends on the user's computer settings and is not reliable.
  • If the event in place with dfferent timezone matches the current position of the user, I must print it inside (). Is it possible in javascript to create a date object with a given time zone and daylight settings?

I also found some workaround, such as jsdate and date web services, but they can not cope with the problem of having a javascript object with the correct time zone and daylight settings (for working with the date, for example, adding days, etc.).

+5
source share
4 answers

A few things to keep in mind.

Store all event dates in UTC

Yes, this cannot be avoided.

Find out all the timestamps ...

... all users in the system. You can use the following detection script: http://site.pageloom.com/automatic-timezone-detection-with-javascript . He will give you the time zone key, for example, America / Phoenix.

In your case, you need to save the time zone along with the event, since the user can switch the time zone, but the event will always occur in a specific one. (Argh)

Choose a display engine

Javascript, ( , script). : https://github.com/mde/timezone-js.

, , :

var dt = new timezoneJS.Date(UTC_TIMESTAMP, 'America/New_York');

var dt = new timezoneJS.Date(2006, 9, 29, 1, 59, 'America/Los_Angeles');

UTC_TIMESTAMP , , 1193855400000. America/New_York - , , .

dt, , JavaScript Date. "" ( DST).

, - , . , , . , UTC → . .

+6

Date. . , 1308150623182 . , . " " .

+2

One possibility would be to use the UTC date and time for everything. Thus, there is nothing to convert.

Another is to provide your server with time and date. Then you do not need to depend on the user to configure it correctly, and you do not need to worry about where your user’s time zone is located.

0
source

Use getUTCDate(), getUTCHours()... instead of getDate(), getHours()... getTimetoneOffset()it can also be useful.

0
source

All Articles