The project needs to use a calendar, and the .net calendar control is too heavy, so I have to write one in js. The core function of the calendar is DateAdd(). During the writing process, I found that the operation time in js is more cumbersome than expected, unlike in vbscript. You can easily dateadd, but later I thought of using built-in functions such as setFullYear() and setDate() to combine a js version of dateadd(). The code is as follows:
function DateAdd(interval,number,date){ // date can be a time object or a string, if For the latter, the format must be: yyyy-mm-dd hh:mm:ss where the delimiter is variable. "December 29, 2006 16:01:23" is also legal
number = parseInt(number);
if (typeof(date)=="string"){
date = date. split(/D/);
--date[1];
eval("var date = new Date(" date.join(",") ")");
}
if (typeof(date)=="object"){
var date = date
}
switch(interval){
case "y": date.setFullYear(date.getFullYear() number ); break;
case "m": date.setMonth(date.getMonth() number); break;
case "d": date.setDate(date.getDate() number); break;
case "w": date.setDate(date.getDate() 7*number); break;
case "h": date.setHours(date.getHour() number); break;
case "n ": date.setMinutes(date.getMinutes() number); break;
case "s": date.setSeconds(date.getSeconds() number); break;
case "l": date.setMilliseconds( date.getMilliseconds() number); break;
}
return date;
}
This function has tried its best to imitate the dateadd function in vbscript, with three parameters , the first one is the changing time interval, which can be year, month, day, week, hour, minute, second, millisecond (extended), the third parameter can be a time object or a string (the form must be: 2006-12-29 14:32:57 or December 29, 2006 14:32:57), the return value of the function is a new time object.