Code jsFiddle: http://jsfiddle.net/14rego/gEUJz/
I’m building an array of dates using these functions. Both work great in webkit browsers, but I know IE is pickier about dates.
nextDayA() works in IE as long as the months don’t include August or September. I spent hours trying to figure it out before deciding to try nextDayB(), which I would think would work on any browser anywhere since it doesn’t include the Date function, but it doesn’t work at all in IE.
I don’t care which one I use, as long as it works. I simply need to get the correct dates in order. I have a bit of experience, but I’m certainly not a guru. Can anyone shed some light?
$.nextDayA = function (year, month, day) {
var jMo = parseInt(month) - 1;
var today = new Date(year, jMo, day);
var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
var nextDy = tomorrow.getDate();
var nextMo = tomorrow.getMonth() + 1;
var nextYr = tomorrow.getFullYear();
if (nextDy < 10) { nextDy = '0' + nextDy; }
if (nextMo < 10) { nextMo = '0' + nextMo; }
var tomDate = parseInt(nextYr + '' + nextMo + '' + nextDy);
return tomDate;
};
$.nextDayB = function (year, month, day) {
var thisYear = parseInt(year);
var thisMonth = parseInt(month);
var thisDay = parseInt(day);
if (year % 4 == 0) {
var leap = 'yes';
} else {
var leap = 'no';
}
var nextDy = thisDay + 1;
if (nextDy > 28 && thisMonth === 2 && leap === 'no') {
var nextDy = 1;
var nextMo = thisMonth + 1;
} else if (nextDy > 29 && thisMonth === 2 && leap === 'yes') {
var nextDy = 1;
var nextMo = thisMonth + 1;
} else if (nextDy > 30 && (thisMonth === 2 || thisMonth === 4 || thisMonth === 6 || thisMonth === 9 || thisMonth === 11)) {
var nextDy = 1;
var nextMo = thisMonth + 1;
} else if (nextDy > 31 && (thisMonth === 1 || thisMonth === 3 || thisMonth === 5 || thisMonth === 7 || thisMonth === 8 || thisMonth === 10 || thisMonth === 12)) {
var nextDy = 1;
var nextMo = thisMonth + 1;
} else {
var nextMo = thisMonth;
}
if (nextMo === 13) {
var nextMo = 1;
var nextYr = thisYear + 1;
} else {
var nextYr = thisYear;
}
if (nextDy < 10) {
nextDy.toString();
var strDy = '0' + nextDy;
} else {
var strDy = nextDy.toString();
}
if (nextMo < 10) {
nextMo.toString();
var strMo = '0' + nextMo;
} else {
var strMo = nextMo.toString();
}
var strYr = nextYr.toString();
var strDate = strYr + '' + strMo + '' + strDy;
var tomDate = parseInt(strDate);
return tomDate;
};
You have to pass a base parameter to
parseInt():If you don’t do that, then strings starting with “0” are interpreted as octal constants, not decimal. Of course, “08” and “09” are meaningless octal constants. The second parameter (10) tells the function that the string should be interpreted as a base 10 string regardless of leading zeros.