JS 日期仅包含 UTC 时区
有没有什么方法可以告诉 Javascript 它不应该使用除 UTC 时区之外的任何东西?
当我创建一个新的 Date 对象时,它会获取我的浏览器时区,但是当通过 JSON 传输时,这会变得混乱。
应用程序中的所有日期和时间都很幼稚,对于用户时区没有用处。因此,仅创建和使用 UTC 时间就可以了,但无论我做什么,我只能得到 UTC 日期的日期,而这还不够好。
我正在使用 Bakcbone 和 DateJS,如果这有什么区别的话。
对此有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
new Date().milliseconds
不传输日期的字符串表示形式。这是 UNIX 时间,ie因此与时区无关。
或者,您可以自己构建日期字符串,但使用
getUTC*< /code>
方法:
Instead of transporting the string representation of the date,
new Date().milliseconds
. This is the UNIX time, i.e.and therefore independent of the timezone.
Alternatively, construct the date string yourself, but use the
getUTC*
methods:我最终只使用 .toString() 并将其与 JSON 帖子一起发送。似乎是最简单的事情。
I ended up just using .toString() and sending that along with the JSON post. Seemed like the simplest thing to do.