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.
Javascript – Compare two dates with JavaScript
comparedatedatetimejavascript
comparedatedatetimejavascript
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.
Best Answer
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 usedate.getTime()
as into be clear just checking for equality directly with the date objects won't work
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.
For the curious,
date.getTime()
documentation: