在 Javascript/ECMAScript 中计算时间跨度
我有一个包含毫秒数的变量,它表示从现在到未来指定点的时间跨度。
我想将这个毫秒数字转换为可以向用户显示的时间跨度值。
我知道我可以通过模算术并手动向用户显示结果来完成此操作,但我想使用 Javascript/ECMAScript 内置的 Date() API 来完成此操作。
这就是我生成它的方式:
var timespanInMS = timeInFuture.getTime() - now.getTime();
var diff = new Date( timespanInMS );
alert( "Hours: " + diff.getHours() + " Minutes: " + diff.getMinutes() );
但是,这只在用户的计算机位于 UTC 时区时才有效。如果它们位于太平洋 (UTC-8),则“diff”的值会相差 16 小时(即使 timespanInMS 数字相同)。
谢谢
I've got a variable that holds a number of miliseconds which represents the timespan from now to a specified point in the future.
I want to convert this milisecond figure into a timespan value I can display to users.
I know I can do this the naive way with modulo arithmetic and manually displaying the result to the user, but I want to do this using the Date() API built-in to Javascript/ECMAScript.
This is how I generate it:
var timespanInMS = timeInFuture.getTime() - now.getTime();
var diff = new Date( timespanInMS );
alert( "Hours: " + diff.getHours() + " Minutes: " + diff.getMinutes() );
However this only works when the user's computer is in the UTC timezone. If they're in Pacific (UTC-8) then the value of 'diff' is off by 16 hours (even though the timespanInMS figure is the same).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许
getUTCHours
和getUTCMinutes
可能有效?请参阅 https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects /日期了解更多信息。Perhaps
getUTCHours
andgetUTCMinutes
might work? See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date for more information.