简体   繁体   English

JS日期仅限UTC时区

[英]JS Dates only with UTC timezone

Is there some way to tell Javascript that it should never use anything but the UTC timezone? 有没有办法告诉Javascript它应该永远不会使用除UTC时区之外的任何东西?

When I create a new Date object, it gets my browsers timezone, but this will muck up when transporting via JSON. 当我创建一个新的Date对象时,它会获得我的浏览器时区,但是当通过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. 因此,创建和使用UTC时间就可以了,但无论我做什么,我只能得到我的日期在UTC中的样子,那还不够好。

I am using Bakcbone and DateJS if that makes any difference. 如果这有任何区别,我正在使用Bakcbone和DateJS。

Any ideas on this? 有什么想法吗?

Instead of transporting the string representation of the date, new Date().milliseconds . 而不是传输日期的字符串表示, new Date().milliseconds This is the UNIX time, ie 这是UNIX时间,

Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC. 整数值,表示自1970年1月1日00:00:00 UTC以来的毫秒数。

and therefore independent of the timezone. 因此独立于时区。

Alternatively, construct the date string yourself, but use the getUTC* methods: 或者,自己构造日期字符串,但使用getUTC*方法:

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. 我最后只使用.toString()并将其与JSON帖子一起发送。 Seemed like the simplest thing to do. 看起来像最简单的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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