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

Add padding to the beginning of a string

const name = 'Minta Lofton';
console.log(name.padStart(13));
console.log(name.padStart(2));

With the padStart method, we can add padding to the beginning of a string. The value passed to this method is the total length of the string together with the padding. The string "Minta Lofton" has a length of 12. name.padStart(13) inserts 1 space at the start of the string, because 12 + 1 is 13.
If the argument passed to the padStart method is smaller than the length of the array, no padding will be added.

Ref: https://bit.ly/3jFRBje