开发者

Javascript date object in different locale and timezone

I need to write a web application that show events of people in different locale. I almost finished it, but there're 2 problems with date:

  • using date javascript object, the date depends on user computer settings and it's not reliable
  • if there's an event in a place with df开发者_StackOverflow社区ferent timezone respect user current position, i have to print it inside (). Is it possible in javascript to build a date object with a given timezone and daylight settings?

I also find some workaround, such as jsdate and date webservices, but they don't overcome the problem of having a javascript object with the correct timezone and daylight settings (for date operation such as adding days and so on).


A couple of things to keep in mind.

Store all event datetimes in UTC time

Yes, there is no getting around this.

Find out all the timezones...

...of all the users in the system. You can use the following detection script: http://site.pageloom.com/automatic-timezone-detection-with-javascript. It will hand you a timezone key such as for example "America/Phoenix".

In your case you need to store the timezone together with the event, since a user may switch timezone - but the event will always have happened in a specific one. (argh)

Choose your display mechanism

If you want to localize your event dates with Javascript, there is a nifty library for that too (which can use the keys supplied with the previous script). Here: https://github.com/mde/timezone-js.

with that library you can for example do this:

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

or

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

where UTC_TIMESTAMP for example could be 1193855400000. And America/New_Yorkis the timezone you have detected when the event took place.

The dt object that you get from this will behave as a normal JavaScript Date object. But will automatically "correct" itself to the timezone you have specified (including DST).

If you want to, you can do all the corrections in the backend - before you serve the page. Since I don't know what programming language you are using there, I cannot give you any immediate tips. But basically it follows the same logic, if you know the timezone, and the UTC datetime -> you can localize the datetime. All programming languages have libraries for that.


You're missing the point of a Date object. It represents a particular point in time. As I speak, it is 1308150623182 all over the world. Timezone only comes into play when you want to display the time to the user. An operation like "adding a day" does not involve the time zone at all.


One possibility might be to use UTC date and time for everything. That way, there is nothing to convert.

Another is to have your server provide the time and date. Then you don't have to depend on the user to have it set correctly, and you don't have to worry about where your user's timezone is.


Use getUTCDate(), getUTCHours(), ... instead of getDate(), getHours(),... getTimetoneOffset() could be useful, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜