javascript - 通过 moment.js 转换后,我得到了错误的日期

标签 javascript angularjs momentjs

我的代码是

console.log(moment(new Date('2016-05-24T18:50:05.000')).format('LL'));

应该是2016年5月24日,但它给了我2016年5月25日

谁能帮帮我。

最佳答案

您需要将日期指定为 UTC。

您可以通过以下任一方式执行此操作:

console.log(moment(new Date('2016-05-24T18:50:05.000Z')).format('LL'));

其中,Z 指定时间为 UTC。或者:

console.log(moment.utc(new Date('2016-05-24T18:50:05.000')).format('LL'));


您可以直接将 ISO 8601 日期字符串传递给 moment。

无需将字符串包装在 javascript Date 对象中。那么你的代码就变成了:

console.log(moment('2016-05-24T18:50:05.000Z').format('LL'));

或者:

console.log(moment.utc('2016-05-24T18:50:05.000').format('LL'));

关于javascript - 通过 moment.js 转换后,我得到了错误的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37444841/

相关文章:

javascript - 在 headless 模式下测试嵌套 iframe 时出现 Cypress 错误 - 竞争条件

javascript - Reactjs更新componentDidMount?

javascript - undefined 不是一个对象(评估 '$scope.students.pay' )

angularjs - 从无效输入字段获取 "raw"值

javascript - Moment.js 格式化时间

javascript - Google Closure Linter 在 Windows 的 Sublime Text 2 中不起作用

javascript - js使用加密图片

AngularJS 在一个 Controller 中更改模型值会触发其他 Controller 中的模型更新

javascript - 如何在不更改日期值的情况下更改时区

javascript - Moment js判断已经过去了多少天,几周