JavaScript Date 对象总是以 America/New_York 时区打印?
我有一个从我的服务器发送的以毫秒为单位的时间戳(Unix 时间/来自 Epoch 的时间),它使用 Joda-time 并始终使用恒定时区“America/New_York”。在我的客户端上,我还想保证时间按照America/New_York时区显示。我依赖于 MDN 日期文档,并认为这是最简单的使用新的 Date(毫秒) 构造函数实例化我的 Date 对象。
现在是困难的部分:我希望能够为在美国/纽约时区之外使用我的网站的客户打印时间。我知道我可以从 Date 对象 getTimezoneOffset /code>,所以现在我正在考虑根据偏移量对该对象执行算术,并依靠服务器来告诉我是否处于夏令时。使用 Joda Time 库中的此方法< /a>.
我想要一个适用于 IE8 以及现代浏览器的解决方案。有什么想法吗?是否有任何小型实用程序已经以标准方式完成此任务?
// dateTimeAsString is sent from the server (via JSON) as a String
// dateTimeAsString example: "1311387300000"
var dateTimeAsInt = parseInt(dateTimeAsString,10);
var dateTimeInNY = new Date(dateTimeAsInt);
// My current offset is 240
var nyOffset = dateTimeInNY.getTimezoneOffset();
// Somewhere out west
var dateTimeInTexas = new Date(dateTimeAsInt);
var isDST = false; // sent from server
// What I want to do, this is not legal JavaScript
dateTimeInTexas.printWithOffset(isDST,nyOffset);
I have a time-stamp in milliseconds (Unix time / time from Epoch) sent from my server, which uses Joda-time and always the constant timezone "America/New_York". On my client, I also want to guarantee time is displayed according to the America/New_York timezone. I've relied on the MDN Date documentation, and have decided it's easiest to instantiate my Date objects using the new Date(milliseconds) constructor.
Now the hard part: I want to be able to print the time for clients that are using my site outside the America/New_York timezone. I know I can getTimezoneOffset from a Date object
, so now I'm considering performing arithmetic on that object based on the offset and relying on the server to tell me if I'm in DST or not. Using this method from the Joda Time library.
I'd like a solution that works in IE8 as well as the modern browsers. Any thoughts? Are there any small utilities that already accomplish this in a standard way?
// dateTimeAsString is sent from the server (via JSON) as a String
// dateTimeAsString example: "1311387300000"
var dateTimeAsInt = parseInt(dateTimeAsString,10);
var dateTimeInNY = new Date(dateTimeAsInt);
// My current offset is 240
var nyOffset = dateTimeInNY.getTimezoneOffset();
// Somewhere out west
var dateTimeInTexas = new Date(dateTimeAsInt);
var isDST = false; // sent from server
// What I want to do, this is not legal JavaScript
dateTimeInTexas.printWithOffset(isDST,nyOffset);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览器没有时区信息。它们只能以 UTC 和当前操作系统时区显示。即使它也不能保证客户端对“America/New_York”时区有任何了解(例如,性能较差、性能较弱的移动设备)。
如果您想显示特定时区的日期,最简单的方法是将其转换为格式化字符串并将其作为字符串而不是毫秒传输。
如果您需要在客户端进行一些日历计算,那么您很不幸......您必须手动进行。
顺便说一句,您不能按原样使用 timezoneOffset,因为它对于特定日期可能有所不同,DST 也很重要。
所以,首先。您想对客户端的日期做什么?
Browsers don't have timezone information. They able to display only in UTC and in current OS timezone. Even it's not guaranteed that a client has any knowledge about "America/New_York" timezone (e.g. a poor, weak mobile device).
If you want display dates in a particular timezone, the easiest way it convert it to a formatted string and transfer it as a string, not milliseconds.
If you need to do some calendar calculations on the client side, you are unlucky... You have to do it manually.
BTW, you cannot use timezoneOffset as is, because it could be different for particular dates, DST is important too.
So, first of all. What do you want to do with dates on the client side?