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 in Java by Aditya
• 7,280 points
18 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 by Rahul
• 8,980 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,900 points
335 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,170 points
723 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
1,770 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,900 points
1,994 views
0 votes
1 answer

Difference between datetime and timestamp in sqlserver?

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

answered Feb 16 in Database by Vaani
• 5,040 points
143 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 in Java by Aditya
• 7,280 points
20 views
0 votes
1 answer

Parsing a string to a date in JavaScript

The best string format for string parsing ...READ MORE

answered Feb 18 in Java by Aditya
• 7,280 points
21 views
0 votes
1 answer

Date difference: different results in Excel vs. Python

To calculate the date difference, we can ...READ MORE

answered Feb 23 in Database by Edureka
• 8,820 points
42 views
0 votes
1 answer

Problem with gif with transparent background

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

answered Feb 8 in Java by Rahul
• 8,980 points
36 views
0 votes
1 answer

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

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

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