Compare Dates in Angular 2

33.4k views Asked by At

I'm trying to figure out how best to compare dates in Angular 2 and TypeScript 1.8.7.

Given:

startDate: Date;
endDate: Date;

if(this.startDate.getUTCMilliseconds() === this.endDate.getUTCMilliseconds()){
     //do stuff here
} else {
     // do something else here
}

This will return an error like "startDate.getUTCMilliseconds is not a function..."

Does somebody have a best practice? Thanks,

3

There are 3 answers

5
kemsky On

Date object method to get milliseconds is called getTime.

2
Matt C On

You can compare Date objects directly, with operators like <, >, and ==.

this.startDate == this.endDate
1
user1027620 On

I know it's irrelevant and not what you asked for, but use angular-moment / momentjs. Dates in JS are a complete mess.