How can I compare a "MM/DD/YYYY" date with the Date() function in Javascript? -
how can compare date input of format "mm/dd/yyyy" date()
function in javascript?
for example:
if (inputdate < todaysdate){ alert("you entered past date") } else if (inputdate > todaysdate){ alert("you entered future date") } else if (inputdate = todaysdate){ alert("you entered present date") } else{ alert("please enter date") }
convert string date using new date(datestring)
. normalize today's date omit time information using today.sethours(0, 0, 0, 0)
. can compare dates have above:
var date = new date(dateinput); if (isnan(date)) { alert("please enter date"); } else { var today = new date(); today.sethours(0, 0, 0, 0); date.sethours(0, 0, 0, 0); var datetense = "present"; if (date < today) { datetense = "past"; } else if (date > today) { datetense = "future"; } alert("you entered " + datetense + " date"); }
Comments
Post a Comment