我得到了一个表示时间的字符串和一个时区ID。我需要确定所讨论的时间是否发生在下半小时内,但我正在运行的计算机所处的时区与捕获该字符串的时区不同。小提示:如果事件发生在过去,则可以(仍然发生===true)。只是想把未来半个多小时内发生的事情和其他事情区别开来。
这似乎应该是直截了当的,但我却一无所获。
const moment = require('moment-timezone')
const hm = s => moment(s).format('HH:mm')
const happensSoon = (then, timezoneId) => {
console.log(`then:`, then) // 2018-10-04T16:39:52-07:00
console.log(`timezoneId:`, timezoneId) // America/New_York
const localNow = moment()
.clone()
.tz(timezoneId)
const localThen = moment(then)
.clone()
.tz(timezoneId)
const diff = localThen.diff(localNow, 'minutes')
console.log(`localThen:`, hm(localThen)) // 19:39
console.log(`localNow:`, hm(localNow)) // 16:24
console.log(`then:`, hm(then)) // 16:39
console.log(`diff:`, diff) // 194
return diff <= 30
}
在“美国/洛杉矶”时区跑步。我的“本地”代表纽约时间。所以,16:39是
then