开发者

JS Dates only with UTC timezone

开发者 https://www.devze.com 2023-04-11 10:06 出处:网络
Is there some way to tell Javascript that it should never use anything but the UTC timezone? When I create a new Date object, it gets my browsers timezone, but this will muck up when transporting via

Is there some way to tell Javascript that it should never use anything but the UTC timezone?

When I create a new Date object, it gets my browsers timezone, but this will muck up when transporting via JSON.

All dates and times in the app are naive and has no use for the users timezone. So creating and working with only UTC times would be just fine, but no matter what I do, I just get what my date would look like in UTC and thats just not good enough.

I am using Bakcbone and DateJS if that makes any difference.

Any开发者_StackOverflow ideas on this?


Instead of transporting the string representation of the date, new Date().milliseconds. This is the UNIX time, i.e.

Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.

and therefore independent of the timezone.

Alternatively, construct the date string yourself, but use the getUTC* methods:

var d = new Date();
alert("It's " + d.getUTCHours() + ':' + d.getUTCSeconds());


I ended up just using .toString() and sending that along with the JSON post. Seemed like the simplest thing to do.

0

精彩评论

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

关注公众号