一個js日期方面好用的函式
日期加法
function DateAdd(timeU,byMany,dateObj){
var millisecond=1;
var second=millisecond*1000;
var minute=second*60;
var hour=minute*60;
var day=hour*24;
var year=day*365;
var newDate;
var dVal=dateObj.valueOf();
switch(timeU) {
case "ms": newDate=new Date(dVal+millisecond*byMany);
break;
case "s": newDate=new Date(dVal+second*byMany);
break;
case "mi": newDate=new Date(dVal+minute*byMany);
break;
case "h": newDate=new Date(dVal+hour*byMany);
break;
case "d": newDate=new Date(dVal+day*byMany);
break;
case "y": newDate=new Date(dVal+year*byMany);
break;
}
return newDate;
}
ex: 想在日期加一天就用, date = DateAdd("d", 1,date);
function DateAdd(timeU,byMany,dateObj){
var millisecond=1;
var second=millisecond*1000;
var minute=second*60;
var hour=minute*60;
var day=hour*24;
var year=day*365;
var newDate;
var dVal=dateObj.valueOf();
switch(timeU) {
case "ms": newDate=new Date(dVal+millisecond*byMany);
break;
case "s": newDate=new Date(dVal+second*byMany);
break;
case "mi": newDate=new Date(dVal+minute*byMany);
break;
case "h": newDate=new Date(dVal+hour*byMany);
break;
case "d": newDate=new Date(dVal+day*byMany);
break;
case "y": newDate=new Date(dVal+year*byMany);
break;
}
return newDate;
}
ex: 想在日期加一天就用, date = DateAdd("d", 1,date);
留言
張貼留言