Following on from our article on adding and subtracting time from dates in JavaScript – here’s how to compare two JavaScript date objects.
This article will explore comparing dates/times with different granularity levels – from dates that are an exact month to dates that fall in the same year.
Using Boolean Operators
Native Javascript date objects can be compared using standard boolean/comparison operators – allowing you to compare dates by checking whether they are equal, not equal, greater than, less than, equal to or greater than, or equal to or less than each other.
Checking if Dates are Identical in Javascript
Check for equality:
var date1 = new Date(); // Defaults to now var date2 = new Date(); // Defaults to now if(date1 == date2) { // Dates are the same }
Checking if A Date is Before or After Another
Simply use standard JavaScript boolean comparison operators:
var date1 = new Date(); // Defaults to now var date2 = new Date(); // Defaults to now date1.setTime(date1.getTime() + (1 * 60 * 60 * 1000)); // Add an hour to date1 if(date1 > date2){ // The time stored by date1 is after date2 } if(date2 < date1){ //The time stored by date2 is before date1 }
When using boolean comparisons on date objects, they are compared down to the millisecond (1/1000 of a second). Here’s how to compare to different time units:
var date1 = new Date(); // Defaults to now var date2 = new Date(); // Defaults to now date1.setTime(date1.getTime() + (1 * 60 * 60 * 1000)); // Add an hour to date1 # The getSeconds() method will return the seconds component of the date object from 0-59 # The getMinutes() method will return the minutes component of the date object from 0-59 # The getHours() method will return the hours component of the date object from 0-23 # The getDate() method will return the date (day in month) of the date object from 0-31 # The getMonth() method will return the month of the date object from 0-11 (Starting with January at 0) # The getFullYear() method will return the year of the date object as a 4 digit number (eg 2021) #So, to see if two dates share the same minute you could run if(date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate() && date1.getHours() == date2.getHours() && date1.getMinutes() == date2.getMinutes() && ){ // Dates are the same down to the minute }
Moment JS
That last example above is a bit messy – there is a better way.
If you build applications that deal with dates frequently, Moment.js is invaluable.
Moment.js provides the tools to manage dates, timezones, time periods (the period between two dates) – all conveniently wrapped in easily used classes. The documentation is great, and it simplifies date handling while making it more reliable.
Find it at:
For example, checking if two dates fall into the same minute, you’d simply run:
date1 = moment(); // Defaults to now date2 = moment().add(7, 'days').add(2, 'minutes'); // Second date is 7 days and 2 minutes into the future date1.isSame(date2, 'minute'); // Returns true if the dates are the same down to the minute