Well I know this question is asked a thousand times on internet but I am a rookie and don’t know much about Javascript functions. I want to compare two date.
Input is in form = “2011-jan-21”
Here is what I have come up, Please help! I will really appreciate because I need to get it worked urgently.
function compareDates() {
var startDate=document.getElementById("startDate").value;
var endDate = document.getElementById("endDate").value;
months = {'jan': '01', 'feb': '02', 'mar': '03', 'apr': '04', 'may': '05',
'jun': '06', 'jul': '07', 'aug': '08', 'sep': '09', 'oct': '10', 'nov': '11',
'dec': '12'};
split = startDate.split('-');
var newStartDate = [split[2], months[split[1]], split[0]].join(',');
split = endDate.split('-');
var newEndDate = [split[2], months[split[1]], split[0]].join(',');
var myDate=new Date();
myDate.setFullYear(startDate);
console.log(newStartDate);
var myDateEnd=new Date();
myDateEnd.setFullYear(endDate);
if (myDate < myDateEnd) {
alert ("Error !");
}
}
<form method="POST" id="myForm" onsubmit="compareDates()">
<input id="startDate" />
<input id="endDate" />
<input type="Submit" />
</form>
Just pass the string to a
Dateconstructor. Much easier.Now you’ve got two full-fledged
Dateinstances that you can work with as you please.