时刻 js 'from' 给出奇怪的结果
moment js 'from' giving weird result
我像这样从后端获取 date
[2020, 8, 5, 13, 29, 43, 780000000]
(这是 UTC 日期)。
我必须将此日期与当前 UTC 进行比较以获得相对时间。
在此示例中,当前 UTC 为:- 2020-08-05T15:59:52.514Z
预计相对时间为 2 小时 30 分钟。我不确定为什么 Moment JS 的实际响应是 16 小时。
const timeInUTC = moment.utc(date)
const currentTimeInUTC = moment.utc(new Date().toISOString())
console.log(timeInUTC.from(currentTimeInUTC))
格式有问题。输入到 moment 的日期是数组格式,而我以“UTC 格式”计算 UTC 中的当前时间。
在确保两者具有相同的格式后解决了这个问题。
const dateInUTCFormat = new Date(
Date.UTC(date.value[0], date.value[1] - 1, date.value[2], date.value[3], date.value[4], date.value[5])
)
const timeInUTC = moment(dateInUTCFormat)
const currentTimeInUTC = moment.utc()
console.log(timeInUTC.from(currentTimeInUTC)
)
我像这样从后端获取 date
[2020, 8, 5, 13, 29, 43, 780000000]
(这是 UTC 日期)。
我必须将此日期与当前 UTC 进行比较以获得相对时间。
在此示例中,当前 UTC 为:- 2020-08-05T15:59:52.514Z
预计相对时间为 2 小时 30 分钟。我不确定为什么 Moment JS 的实际响应是 16 小时。
const timeInUTC = moment.utc(date)
const currentTimeInUTC = moment.utc(new Date().toISOString())
console.log(timeInUTC.from(currentTimeInUTC))
格式有问题。输入到 moment 的日期是数组格式,而我以“UTC 格式”计算 UTC 中的当前时间。
在确保两者具有相同的格式后解决了这个问题。
const dateInUTCFormat = new Date(
Date.UTC(date.value[0], date.value[1] - 1, date.value[2], date.value[3], date.value[4], date.value[5])
)
const timeInUTC = moment(dateInUTCFormat)
const currentTimeInUTC = moment.utc()
console.log(timeInUTC.from(currentTimeInUTC)
)