How to Compare Two Dates in JavaScript Without Time

To compare two dates in JavaScript without considering the time, set the time to midnight using setHours(0, 0, 0, 0) on both dates and then compare them.

let date1 = new Date('2023-07-16');
let date2 = new Date('2023-07-16');

date1.setHours(0, 0, 0, 0);
date2.setHours(0, 0, 0, 0);

let isSameDate = date1.getTime() === date2.getTime();
console.log(isSameDate); // true

Comparing dates without considering the time component is a common requirement in JavaScript, especially for applications dealing with schedules, events, or deadlines. This guide explores various methods to compare two dates while ignoring the time part.

Using setHours Method

The setHours method sets the hours, minutes, seconds, and milliseconds of a date to zero, effectively normalizing the date to midnight.

Example 1: Using setHours Method

let date1 = new Date('2023-07-16T12:34:56');
let date2 = new Date('2023-07-16T23:45:00');

date1.setHours(0, 0, 0, 0);
date2.setHours(0, 0, 0, 0);

let isSameDate = date1.getTime() === date2.getTime();
console.log(isSameDate); // true

Explanation:

  • let date1 = new Date('2023-07-16T12:34:56'); Initializes the first date with a specific time.
  • let date2 = new Date('2023-07-16T23:45:00'); Initializes the second date with a different time.
  • date1.setHours(0, 0, 0, 0); Sets the time of date1 to midnight.
  • date2.setHours(0, 0, 0, 0); Sets the time of date2 to midnight.
  • let isSameDate = date1.getTime() === date2.getTime(); Compares the timestamps of both dates.
  • console.log(isSameDate); Outputs true since both dates are the same when the time is ignored.

Using Date String Comparison

Another method is to format the dates as strings in YYYY-MM-DD format and compare them directly.

Example 2: Using Date String Comparison

let date1 = new Date('2023-07-16T12:34:56');
let date2 = new Date('2023-07-16T23:45:00');

let dateStr1 = date1.toISOString().split('T')[0];
let dateStr2 = date2.toISOString().split('T')[0];

let isSameDate = dateStr1 === dateStr2;
console.log(isSameDate); // true

Explanation:

  • let date1 = new Date('2023-07-16T12:34:56'); Initializes the first date with a specific time.
  • let date2 = new Date('2023-07-16T23:45:00'); Initializes the second date with a different time.
  • let dateStr1 = date1.toISOString().split('T')[0]; Converts date1 to ISO string format and extracts the date part.
  • let dateStr2 = date2.toISOString().split('T')[0]; Converts date2 to ISO string format and extracts the date part.
  • let isSameDate = dateStr1 === dateStr2; Compares the date strings.
  • console.log(isSameDate); Outputs true since both dates are the same when the time is ignored.

Using Moment.js Library

Moment.js is a popular library for date manipulation in JavaScript, providing a simple way to compare dates without time.

Example 3: Using Moment.js

First, include Moment.js in your project:

<script src="https://cdn.jsdelivr.net/npm/moment/min/moment.min.js"></script>

Then use Moment.js to compare the dates:

let date1 = moment('2023-07-16T12:34:56');
let date2 = moment('2023-07-16T23:45:00');

let isSameDate = date1.isSame(date2, 'day');
console.log(isSameDate); // true

Explanation:

  • let date1 = moment('2023-07-16T12:34:56'); Initializes the first date with a specific time using Moment.js.
  • let date2 = moment('2023-07-16T23:45:00'); Initializes the second date with a different time using Moment.js.
  • let isSameDate = date1.isSame(date2, 'day'); Uses Moment.js isSame method to compare the dates, ignoring the time.
  • console.log(isSameDate); Outputs true since both dates are the same when the time is ignored.

Handling Edge Cases

When comparing dates, it is crucial to handle edge cases such as invalid date formats or null values.

Example 4: Handling Edge Cases

function areDatesEqual(date1, date2) {
  if (!date1 || !date2) return false;
  if (isNaN(date1.getTime()) || isNaN(date2.getTime())) return false;
  
  date1.setHours(0, 0, 0, 0);
  date2.setHours(0, 0, 0, 0);

  return date1.getTime() === date2.getTime();
}

let date1 = new Date('2023-07-16T12:34:56');
let date2 = new Date('2023-07-16T23:45:00');
console.log(areDatesEqual(date1, date2)); // true

let invalidDate = new Date('invalid-date');
console.log(areDatesEqual(date1, invalidDate)); // false

Explanation:

  • function areDatesEqual(date1, date2) { ... } Defines a function to compare two dates, handling edge cases.
  • if (!date1 || !date2) return false; Checks if either date is null or undefined.
  • if (isNaN(date1.getTime()) || isNaN(date2.getTime())) return false; Checks if either date is invalid.
  • date1.setHours(0, 0, 0, 0); Sets the time of date1 to midnight.
  • date2.setHours(0, 0, 0, 0); Sets the time of date2 to midnight.
  • return date1.getTime() === date2.getTime(); Compares the timestamps of both dates.
  • console.log(areDatesEqual(date1, date2)); Outputs true since both dates are the same when the time is ignored.
  • console.log(areDatesEqual(date1, invalidDate)); Outputs false since one of the dates is invalid.

Conclusion

Comparing dates in JavaScript without considering the time can be accomplished using various methods, each suitable for different scenarios. Whether using the setHours method, string comparison, Moment.js library, or handling edge cases, JavaScript provides robust tools for date manipulation. Understanding these techniques allows you to choose the most appropriate method for your specific needs, ensuring your code is efficient and effective.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top