使用moment.js时间不能按预期工作 [英] Time not working as expected using moment.js
问题描述
我正在开发一个Web应用程序,我想查找不同时区的本地时间。我正在使用 moment.js 来完成此任务。问题是返回时间不能按预期工作。例如:
I am working on a web application where i want to find the local time for different time zones. I am using moment.js in order to accomplish this task. The problem is that returning time does not work as expected. For example:
moment().tz("Europe/London")._d;
将返回 2013年8月15日星期四01:22:23 GMT + 0300(GTB日光时间) )但同时在伦敦的当地时间是 2013年8月14日星期四22:22:23 所以有3小时的差异。谁能解释为什么会发生这种情况并提出解决方案?谢谢。
will return Thu Aug 15 2013 01:22:23 GMT+0300 (GTB Daylight Time) but at the same time local hour at London is Thu Aug 14 2013 22:22:23 so there is a 3 hour difference. Can anyone explain why this is happening and suggest a solution? Thank you.
推荐答案
请勿使用 ._ d
属性。下划线表示它是内部的。 (因为这是一个日期
,它总是会显示在本地时区,因为这就是JavaScript的工作方式。)
Don't use the ._d
property. The underscore is there to indicate that it's internal. (Since that is a Date
, it will always show as if it were in the local time zone, because that's how JavaScript works.)
而是使用其中一种输出方法,例如 .format()
。
Instead use one of the output methods, such as .format()
.
这篇关于使用moment.js时间不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!