Borislav Hadzhiev
Sun Jan 23 2022·3 min read
Photo by Joshua Rawson-Harris
To compare dates without time:
setUTCHours()
method to set the time on the copied dates to
midnight.getTime()
method on the dates.const date1 = new Date('2022-01-23T06:55:31.820Z'); const date2 = new Date('2022-01-23T09:30:24.820Z'); const date1WithoutTime = new Date(date1.getTime()); const date2WithoutTime = new Date(date2.getTime()); date1WithoutTime.setUTCHours(0, 0, 0, 0); date2WithoutTime.setUTCHours(0, 0, 0, 0); if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) { // ✅ This runs 👇️ (dates are the same) console.log('dates are the same'); } else { console.log('dates are not the same'); } if (date1WithoutTime.getTime() > date2WithoutTime.getTime()) { console.log('date1 comes after date2'); } else if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) { // ✅ This runs 👇️ (dates are the same) console.log('date1 and date2 are the same'); } else { console.log('date2 comes after date1'); }
We used the
Date()
constructor to create 2 Date
objects that both store the 23rd of
January, 2022.
The
setUTCHours
method takes the hours
, minutes
, seconds
and milliseconds
as parameters
and sets the values on the given date according to universal time.
setUTCHours
method mutates the Date
object in place, which might not be what you want.This is why we used the getTime method to get a timestamp of the elapsed milliseconds between the 1st of January 1970 00:00:00 and the given date.
We can pass the timestamp to the Date()
constructor to create a copy of the
Date
object.
setUTCHours
method to set the time for the dates to midnight, so we can compare the dates ignoring the time.The getTime
method returns a number that represents the milliseconds elapsed
between the Unix Epoch and the given date.
// 👇️ 1642944933161 console.log(new Date().getTime());
If we have two dates that store the same year, month and day of the month, then the timestamps are going to be equal because we set the time on both dates to midnight.
You can compare the output from getTime()
just like you would compare numbers
in JavaScript.
Here is an example where date1
stores a value for the 23rd of February, 2022
and date2
stores a value for the 27th of April, 2022.
const date1 = new Date('2022-02-23T06:55:31.820Z'); const date2 = new Date('2022-04-27T09:30:24.820Z'); const date1WithoutTime = new Date(date1.getTime()); const date2WithoutTime = new Date(date2.getTime()); date1WithoutTime.setUTCHours(0, 0, 0, 0); date2WithoutTime.setUTCHours(0, 0, 0, 0); if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) { console.log('dates are the same'); } else { // ✅ This runs 👇️ (dates are NOT the same) console.log('dates are not the same'); } if (date1WithoutTime.getTime() > date2WithoutTime.getTime()) { console.log('date1 comes after date2'); } else if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) { console.log('date1 and date2 are the same'); } else { // ✅ This runs 👇️ (date2 comes after date1) console.log('date2 comes after date1'); }
The value in the date2
variable comes after the one in the date1
variable,
therefore its timestamp is a greater number, so the else
statement runs in
both conditionals.