javascript Moment.js 返回错误的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19648688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:20:35  来源:igfitidea点击:

Moment.js returning wrong date

javascriptdatetimemomentjs

提问by Pan Wangperawong

I'm trying to use moment to convert Date-Time for October 29th, 2013 with this format

我正在尝试使用 moment 以这种格式转换 2013 年 10 月 29 日的日期时间

2013-10-29T00:00:00.000Z

However when I do this

但是,当我这样做时

moment('2013-10-29T00:00:00.000Z').format("MMM Do, YYYY")

It returns October 28th, 2013when it should return October 29th, 2013

它返回2013 年 10 月 28 日,而它应该返回2013 年 10 月 29 日

If you have some ideas on how I can solve this issue, please let me know. Thank you

如果您对我如何解决这个问题有一些想法,请告诉我。谢谢

回答by Jeff Storey

If you want the time in utc, use:

如果您想要 UTC 时间,请使用:

moment.utc('2013-10-29T00:00:00.000')

As @MattJohnson pointed out, using the momentconstructor will translate it to local time. Instead (if you don't want to use the utcmethod), you can replace the Zwith +0. See options for string date/time http://momentjs.com/docs/#/parsing/string-format/

正如@MattJohnson 指出的那样,使用moment构造函数会将其转换为本地时间。相反,(如果你不希望使用的utc方法),你可以代替Z+0。查看字符串日期/时间的选项http://momentjs.com/docs/#/parsing/string-format/

回答by kalley

You can adjust the timezone settings by doing this:

您可以通过执行以下操作来调整时区设置:

moment('2013-10-29T00:00:00.000Z').zone(0).format("MMM Do, YYYY");

FIDDLE

小提琴

Going to go ahead and add Matt's suggestion as well, since it is more semantic.

继续并添加 Matt 的建议,因为它更具语义。

moment('2013-10-29T00:00:00.000Z').utc().format("MMM Do, YYYY");