首页 > 解决方案 > 如何在 momentjs 中获取 endOf day toISOString() 但毫秒应该是 .000Z 而不是 .999Z

问题描述

基本上我有一个约会:

const now = moment()
console.log(now.endOf('day').toISOString()) // 2021-10-25T21:59:59.999Z

我希望它为:2021-10-25T21:59:59.000Z

作为一个走动我做了

console.log(`${moment().endOf('day').utc().format('YYYY-MM-DDTHH:mm:ss')}.000Z`)

得到了我想要的打印输出,但是

也许有人有更优雅的方式?

感谢人们!

标签: reactjsangularreact-nativevue.jsmomentjs

解决方案


或者,您可以将 the 转换moment为 a Date(或仅使用Date它自己),并使用Date.prototype.setUTCHours()with0作为第四个参数(毫秒):

const now = new Date()
now.setUTCHours(23,59,59,0) // end of day
console.log(now.toISOString())


推荐阅读