w3resource

JavaScript: Comparison between two dates

JavaScript Datetime: Exercise-5 with Solution

Write a JavaScript function to compare dates (i.e. greater than, less than or equal to).

Test Data:
console.log(compare_dates(new Date('11/14/2013 00:00'), new Date('11/14/2013 00:00')));
console.log(compare_dates(new Date('11/14/2013 00:01'), new Date('11/14/2013 00:00')));
console.log(compare_dates(new Date('11/14/2013 00:00'), new Date('11/14/2013 00:01')));
Output :
"Date1 = Date2"
"Date1 > Date2"
"Date2 > Date1"

Sample Solution-1:

JavaScript Code:

var compare_dates = function(date1,date2){
     if (date1>date2) return ("Date1 > Date2");
   else if (date1<date2) return ("Date2 > Date1");
   else return ("Date1 = Date2"); 
  }

console.log(compare_dates(new Date('11/14/2013 00:00'), new Date('11/14/2013 00:00')));
console.log(compare_dates(new Date('11/14/2013 00:01'), new Date('11/14/2013 00:00')));
console.log(compare_dates(new Date('11/14/2013 00:00'), new Date('11/14/2013 00:01')));

Sample Output:

Date1 = Date2
Date1 > Date2
Date2 > Date1

Flowchart:

Flowchart: JavaScript- Comparison between two dates

Sample Solution-2:

Checks if a date is the same as another date.

  • Use Date.prototype.toISOString() and strict equality checking (===) to check if the first date is the same as the second one.

JavaScript Code:

const is_same_date = (date1, date2) => date1.toISOString() === date2.toISOString();
console.log(is_same_date(new Date(2015, 12, 15), new Date(2015, 12, 15)));
console.log(is_same_date(new Date(2015, 12, 15), new Date(2015, 12, 16)));

Sample Output:

true
false

Sample Solution-3:

Checks if a date is before another date.

  • Use the less than operator (<) to check if the first date comes before the second one.

JavaScript Code:

const is_before_date = (date1, date2) => date1 < date2;
console.log("Is 2018/8/15 before 2018/8/21?")
console.log(is_before_date(new Date(2018, 8, 15), new Date(2018, 8, 21)));
console.log("Is 2018/8/21 before 2018/8/15?")
console.log(is_before_date(new Date(2018, 8, 21), new Date(2018, 8, 15)));

Sample Output:

Is 2018/8/15 before 2018/8/21?
true
Is 2018/8/21 before 2018/8/15?
false

Sample Solution-4:

Checks if a date is after another date.

  • Use the greater than operator (>) to check if the first date comes after the second one.

JavaScript Code:

const is_after_date = (date1, date2) => date1 > date2;
console.log("Is 2018/8/15 after 2018/8/21?")
console.log(is_after_date(new Date(2018, 8, 15), new Date(2018, 8, 21)));
console.log("Is 2018/8/21 after 2018/8/15?")
console.log(is_after_date(new Date(2018, 8, 21), new Date(2018, 8, 15)));

Sample Output:

Is 2018/8/15 after 2018/8/21?
false
Is 2018/8/21 after 2018/8/15?
true

Live Demo:

See the Pen JavaScript - Comparison between two dates-date-ex- 5 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript function to get the month name from a particular date.
Next: Write a JavaScript function to add specified minutes to a Date object.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

Promise.all method

const promise1 = Promise.resolve('First')
const promise2 = Promise.resolve('Second')
const promise3 = Promise.reject('Third')
const promise4 = Promise.resolve('Fourth')

const runPromises = async () => {
	const res1 = await Promise.all([promise1, promise2])
	const res2  = await Promise.all([promise3, promise4])
	return [res1, res2]
}

runPromises()
	.then(res => console.log(res))
	.catch(err => console.log(err))

The Promise.all method runs the passed promises in parallel. If one promise fails, the Promise.all method rejects with the value of the rejected promise. In this case, promise3 rejected with the value "Third". We're catching the rejected value in the chained catch method on the runPromises invocation to catch any errors within the runPromises function. Only "Third" gets logged, since promise3 rejected with this value.