我的代码是
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/