buruguduys
buruguduys

Reputation: 13

Compare two dates using valueOf()

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

When using valueOf(), this.card.dateScheduled.valueOf() this gives me a value of the actual date. Not the millisecond relative to 1970 (a.k.a the Unix timestamp/epoch).

Why is that?

Upvotes: 1

Views: 180

Answers (2)

Kaushik
Kaushik

Reputation: 2090

I think you can take the benefits of inbuilt functionality.

Here is an example to compare dates in 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);

Upvotes: 0

Ilarion Halushka
Ilarion Halushka

Reputation: 2343

In moment.js there are many useful methods for comparing dates like isAfter, isBefore. So in your case use:

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

Upvotes: 1

Related Questions