Edit: I tried some of the suggestions at the answer below, but I still don't get the appropriate results. Any further help would be appreciated.
I have implemented a form in HTML that takes 2 dates as inputs and they are appended into two arrays. Then I calculate the difference between the 2 dates in months.
a) The form asks the user about his/her five year history and therefore the user should be able to input dates until he/she riches five years(60 months). For this issue, I tried the function below:
if (typeof startArray[index] !== 'null' && endArray[index] !== null) {
var result = difference_between(dateFrom, dateTo);
document.getElementById("txtResult").value = result
if(result <= 60) {
document.getElementById('datepicker1').reset();
document.getElementById('datepicker2').reset();
document.getElementById('txtResult').reset();
}
else{
alert("Go to the next section");
document.getElementById('btnSave').disable = true;
}
b) The form should be able to calculate the gap between the end date and the next start date and there should not be a gap of more than two months. For example, the form should be able to compare the date I ended school with the date I started uni and if it's bigger than 2 months, then I should thow an error message. Here is my function:
function GetDifference() {
var dateFrom = new Date(document.getElementById("datepicker1").value);
var dateTo = new Date(document.getElementById("datepicker2").value);
document.getElementById("txtResult").value = difference_between(dateFrom, dateTo);
//Is there a gap of more than 2 months?
if (endDate.length && (difference_between(endDate[endDate.length - 1], dateFrom)) > 2) {
window.alert("There should not be a gap of more than two months in this section.");
}
}
here is the JSFIDDLE: http://jsfiddle.net/stelios13/KsuxV/111/
Thank you.