Compare two dates with JavaScript

0 votes
Can someone suggest a way to compare the values of two dates greater than, less than, and not in the past using JavaScript? The values will be coming from text boxes.
Feb 18, 2022 in Java by Aditya
• 7,680 points
729 views

1 answer to this question.

0 votes

 The Date object will do what you want - construct one for each date, then compare them using the >, <, <= or >=.

The ==, !=, ===, and !== operators require you to use date.getTime() as in
var d1 = new Date(); 
var d2 = new Date(d1); 
var same = d1.getTime() === d2.getTime(); 
var notSame = d1.getTime() !== d2.getTime();

to be clear just checking for equality directly with the date objects won't work

var d1 = new Date(); 
var d2 = new Date(d1); 

console.log(d1 == d2); // prints false (wrong!) 
console.log(d1 === d2); // prints false (wrong!) 
console.log(d1 != d2); // prints true (wrong!) 
console.log(d1 !== d2); // prints true (wrong!) 
console.log(d1.getTime() === d2.getTime()); // prints true (correct)

I suggest you use drop-downs or some similar constrained form of date entry rather than text boxes, though, lest you find yourself in input validation hell.

answered Feb 18, 2022 by Rahul
• 9,680 points

Related Questions In Java

0 votes
4 answers

How can we compare dates in java?

public static String daysBetween(String day1, String day2) ...READ MORE

answered Sep 5, 2018 in Java by Sushmita
• 6,920 points
1,380 views
0 votes
1 answer

How can I calculate number of days between two dates?

You may refer this. This might work ...READ MORE

answered Jul 19, 2018 in Java by Akrati
• 3,190 points
1,234 views
+1 vote
1 answer

How to calculate days between two dates?

You are making some conversions with your ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,830 points
2,886 views
0 votes
2 answers

How to parse/format dates with LocalDateTime? (Java 8)

Converting LocalDateTime to Time Zone ISO8601 String LocalDateTime ...READ MORE

answered Dec 6, 2018 in Java by Sushmita
• 6,920 points
4,405 views
0 votes
1 answer

Get current date in DD-Mon-YYY format in JavaScript/Jquery

Get Current Date In DD-Mon-YYY Format In ...READ MORE

answered Jun 28, 2022 in Web Development by rajatha
• 7,680 points
1,272 views
0 votes
1 answer

Get current date in DD-Mon-YYY format in JavaScript/Jquery

get current date in jquery in dd/mm/yyyy ...READ MORE

answered Jun 27, 2022 in Web Development by rajatha
• 7,680 points
6,263 views
0 votes
1 answer

Difference between datetime and timestamp in sqlserver?

Timestamp is a synonym for rowversion, according ...READ MORE

answered Feb 16, 2022 in Database by Vaani
• 7,070 points
4,909 views
0 votes
1 answer

How do I get the current date in JavaScript?

To ensure that you get the current ...READ MORE

answered Feb 18, 2022 in Java by Aditya
• 7,680 points
583 views
0 votes
1 answer

Problem with gif with transparent background

In this case, I have noticed that ...READ MORE

answered Feb 8, 2022 in Java by Rahul
• 9,680 points
863 views
0 votes
1 answer

What does "javascript:void(0)" mean?

The href of the link helps with ...READ MORE

answered Feb 8, 2022 in Java by Rahul
• 9,680 points
932 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP