简体   繁体   中英

JS Dates only with UTC timezone

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 ideas on this?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM