I found that JavaScript can return a correct date when you use new Date(nYear, nMonth, nDate);
with the over days of that month.
Try to see the result of a dDate
variable when you use this:
var dDate = new Date(2012, 0, 34); // the result is 3 Feb 2012
I have a SkipDate function to share:
function DaysOfMonth(nYear, nMonth) {
switch (nMonth) {
case 0: // January
return 31; break;
case 1: // February
if ((nYear % 4) == 0) {
return 29;
}
else {
return 28;
};
break;
case 2: // March
return 31; break;
case 3: // April
return 30; break;
case 4: // May
return 31; break;
case 5: // June
return 30; break;
case 6: // July
return 31; break;
case 7: // August
return 31; break;
case 8: // September
return 30; break;
case 9: // October
return 31; break;
case 10: // November
return 30; break;
case 11: // December
return 31; break;
}
};
function SkipDate(dDate, skipDays) {
var nYear = dDate.getFullYear();
var nMonth = dDate.getMonth();
var nDate = dDate.getDate();
var remainDays = skipDays;
var dRunDate = dDate;
while (remainDays > 0) {
remainDays_month = DaysOfMonth(nYear, nMonth) - nDate;
if (remainDays > remainDays_month) {
remainDays = remainDays - remainDays_month - 1;
nDate = 1;
if (nMonth < 11) { nMonth = nMonth + 1; }
else {
nMonth = 0;
nYear = nYear + 1;
};
}
else {
nDate = nDate + remainDays;
remainDays = 0;
};
dRunDate = Date(nYear, nMonth, nDate);
}
return new Date(nYear, nMonth, nDate);
};