【发布时间】:2011-09-22 16:23:02
【问题描述】:
我的服务器发送了一个以毫秒为单位的时间戳 (Unix time / time from Epoch),该服务器使用 Joda-time 并且始终使用恒定时区“America/New_York”。在我的客户端上,我还想保证根据 America/New_York 时区显示时间。我依赖MDN Date documentation,并决定使用 new Date(milliseconds) 构造函数来实例化我的 Date 对象是最简单的。
现在是最困难的部分:我希望能够为在美国/纽约时区以外使用我的网站的客户打印时间。我知道我可以从Date object 获得getTimezoneOffset,所以现在我正在考虑根据偏移量对该对象执行算术运算,并依靠服务器来告诉我我是否在 DST 中。使用this method from the Joda Time library。
我想要一个适用于 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);
【问题讨论】:
标签: java javascript timezone jackson jodatime