使用valueOf()比较两个日期

时间:2019-04-24 05:55:52

标签: javascript angularjs typescript date

 const startDayOfTheWeek: number = moment().startOf('isoweek' as moment.unitOfTime.StartOf).valueOf();
 if (this.card.dateScheduled.valueOf() < startDayOfTheWeek) {
     this.card.dateScheduled = this.card.dateDue;
 }

使用valueOf()时,this.card.dateScheduled.valueOf()为我提供了实际日期的值。不是相对于1970年的毫秒数(也就是Unix时间戳/纪元)。

那是为什么?

2 个答案:

答案 0 :(得分:0)

在moment.js中,有许多有用的方法可用于比较日期,例如isAfterisBefore。因此,在您的情况下使用:

if (moment(this.card.dateScheduled).isBefore(startDayOfTheWeek))

答案 1 :(得分:0)

我认为您可以利用内置功能的好处。

以下是比较javascript中日期的示例。

const first = +new Date(); // current date
const last = +new Date(2014, 10, 10); // old date 

console.log(first);
console.log(last);
// comparing the dates
console.log(first > last);
console.log(first < last);

相关问题