开发者

JavaScript Date object always printed in America/New_York timezone?

I have a time-stamp in milliseconds (Unix time / time from Epoch) sent from my server, which uses Joda-time and always the constant timezone "America/New_York". On my client, I also want to guarantee开发者_Python百科 time is displayed according to the America/New_York timezone. I've relied on the MDN Date documentation, and have decided it's easiest to instantiate my Date objects using the new Date(milliseconds) constructor.

Now the hard part: I want to be able to print the time for clients that are using my site outside the America/New_York timezone. I know I can getTimezoneOffset from a Date object, so now I'm considering performing arithmetic on that object based on the offset and relying on the server to tell me if I'm in DST or not. Using this method from the Joda Time library.

I'd like a solution that works in IE8 as well as the modern browsers. Any thoughts? Are there any small utilities that already accomplish this in a standard way?

// dateTimeAsString is sent from the server (via JSON) as a String
// dateTimeAsString example: "1311387300000"
var dateTimeAsInt = parseInt(dateTimeAsString,10);
var dateTimeInNY = new Date(dateTimeAsInt);

// My current offset is 240
var nyOffset = dateTimeInNY.getTimezoneOffset();

// Somewhere out west
var dateTimeInTexas = new Date(dateTimeAsInt);


var isDST = false; // sent from server


// What I want to do, this is not legal JavaScript
dateTimeInTexas.printWithOffset(isDST,nyOffset);


Browsers don't have timezone information. They able to display only in UTC and in current OS timezone. Even it's not guaranteed that a client has any knowledge about "America/New_York" timezone (e.g. a poor, weak mobile device).

If you want display dates in a particular timezone, the easiest way it convert it to a formatted string and transfer it as a string, not milliseconds.

If you need to do some calendar calculations on the client side, you are unlucky... You have to do it manually.

BTW, you cannot use timezoneOffset as is, because it could be different for particular dates, DST is important too.

So, first of all. What do you want to do with dates on the client side?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜