【问题标题】:MomentJS .format() produces correct date -1 day in my timezoneMomentJS .format() 在我的时区产生正确的日期 -1 天
【发布时间】:2013-01-06 10:41:30
【问题描述】:

我注意到,当我使用 momentJs 在我的时区以特定格式输出日期时,会丢失一天。

我在 UTC-4:30 时区。

这并非在所有时区都会发生;注意到这在 UTC-5:00 时区可以正常工作。

以下是console.log连续运行的结果:


正确的输出:

moment([2013, 0, 24])

瞬间
_a: 数组[8]
_d:2013 年 1 月 24 日星期四 00:00:00 GMT-0430(委内瑞拉标准时间)
_isUTC:假
_lang: 假
_proto:对象

moment([2013, 0, 24]).toDate()

2013 年 1 月 24 日星期四 00:00:00 GMT-0430(委内瑞拉标准时间)


输出不正确:

moment('01/24/2013').format('MM/DD/YYYY')

“2013 年 1 月 23 日”

moment([2013, 0, 24]).format('MM/DD/YYYY')

“2013 年 1 月 23 日”


请注意,一旦使用了 .format() 方法,就会丢失一天,并输出 23 号而不是 24 号。


谁能解释这种行为?同样,它似乎特定于时区。

【问题讨论】:

    标签: javascript jquery momentjs


    【解决方案1】:

    我将我的时区设置为(UTC-4:30) Caracas,然后检查了以下值:

    moment('01/24/2013').format('MM/DD/YYYY')
    

    它正确返回了01/24/2013,所以我无法重现您描述的问题。

    我正在检查 moment.js 2.0.0。在 Windows 8 上运行的 Google Chrome 27.0.1453.116m 中。

    也许这与较旧版本的 moment.js 有关。可以验证一下吗?

    【讨论】:

    • 你在哪里设置时区?
    • @daydreamer - 在 Windows 操作系统时区设置中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 2018-03-08
    • 2016-05-20
    相关资源
    最近更新 更多