0

I need to check the current date with the date we enter in the input element.

Html code:

<input type="date" name="date" id="date>

I can get the today's date by the following code in javascript:

function todayDate() {
   var today = new Date();
   var dd = String(today.getDate()).padStart(2, '0');
   var mm = String(today.getMonth() + 1).padStart(2, '0'); 
   var yyyy = today.getFullYear();
   let date=dd+'/'+mm+'/'+yyyy;
   return date;
 }

Now how to check the input date with the current date?

sibabrat swain
  • 1,277
  • 8
  • 20
loksan
  • 157
  • 3
  • 17

1 Answers1

1

I try this using Date Object. maybe it will help you. first, I set both in Date object and then set time to 00:00:00:00 | hh:mm:ss:ms. then check using Date getTime function to both are same or not.

function checkDate(e){
var value = e.target.value
var today = new Date()
today.setHours(0,0,0,0) //Reset current Date Time
console.log(value)
var inputDate= new Date(value)
inputDate.setHours(0,0,0,0) // Reset selected Date Time
  if(inputDate.getTime() == today.getTime()){
    console.log('Current Date Selected')
  }
}
<input type="date" name="date" id="date" onChange="checkDate(event)">
Nilesh Chavan
  • 361
  • 3
  • 10