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开发者_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.
精彩评论