Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript how to check if a date is greater than or equal to a date value [duplicate]

Tags:

javascript

I'm currently trying to see if the date value is set to today's date or greater.

var date = document.getElementById("inputDate").value;
var varDate = new Date(date); //dd-mm-YYYY
var today = new Date();

if(varDate >= today) {
//Do something..
alert("Working!");
}

Date format dd-mm-YYYY

The current code above won't work.

like image 432
Mattlinux1 Avatar asked May 27 '16 14:05

Mattlinux1


People also ask

How to check whether given date is greater than current date?

The following example code checks whether given date is greater than current date or today using with new Date () in JavaScript. var GivenDate = '2018-02-22' ; var CurrentDate = new Date (); GivenDate = new Date (GivenDate); if (GivenDate > CurrentDate) { alert ( 'Given date is greater than the current date.' );

How to validate a date in JavaScript?

Among various kind of data validation, validation of date is one. 1. dd/mm/yyyy or dd-mm-yyyy format. 2. mm/dd/yyyy or mm-dd-yyyy format. In the following examples, a JavaScript function is used to check a valid date format against a regular expression.

How do I check if a date comes after another date?

Checks if a date is after another date. Use the greater than operator (>) to check if the first date comes after the second one.

How to instantiate a date object in JavaScript?

JavaScript Date Object provides a simple way to instantiate a date. The following example code checks whether given date is greater than current date or today using with new Date () in JavaScript.


1 Answers

If you only want to compare Date and not Date-Time then add this line

var today = new Date(); today.setHours(0,0,0,0); 

before comparison.

like image 168
gurvinder372 Avatar answered Sep 18 '22 03:09

gurvinder372