JS片刻检查现在是否在一分钟后不起作用

时间:2018-01-15 09:49:34

标签: javascript momentjs

我需要根据前一次检查当前时间。

const created = moment(new Date());
const oneMinuteLater = created.add(1, 'minutes');

setTimeout(() => {
  const now = moment();
  // Returns false
  console.log('oneMinutePass', now.isAfter(oneMinuteLater, 'minute'));
}, 60000);

如果一分钟过去,为什么它会返回false?

1 个答案:

答案 0 :(得分:3)

一分钟后,now 等于oneMinuteLater

开始

now = 15/01/2018 11:08:00
oneMinuteLater = 15/01/2018 11:08:01

一分钟后

now = 15/01/2018 11:08:01
oneMinuteLater = 15/01/2018 11:08:01
//now === oneMinuteLater

解决方案是使用isSameOrAfter()