JavaScript中日期时间函数的扩展
2009年12月13日 19:09 by webphoenix
JS中的日期时间的函数就那么几个,用起来还是有点不方便,我很久之前写了几个,个人感觉还不错,供大家参考
/*******************************************************************
* 功能: 在当前时间的基础增加或减少一个时间值,变成另一个日期,类似VBS里的DateAdd
* 输入: flag:标识, yyyy表示年,q表示季度,ww表示周,m表示月,d表示天,h表示小时,n表示分,s表示秒, num:差值
* 输出: 增减之后的日期值 Date类型
*******************************************************************/
Date.prototype.dateAdd = function(flag,num) {
var year = this.getFullYear();
var month = this.getMonth();
var day = this.getDate();
var hour = this.getHours();
var minute = this.getMinutes();
var second = this.getSeconds();
var milliSecond = this.getMilliseconds();
num = isNaN(num=parseInt(num))?0:num;
switch(flag) {
case "yyyy":
return new Date(year+num,month,day,hour,minute,second,milliSecond);
case "q":
return new Date(year,month+4*num,day,hour,minute,second,milliSecond);
case "m":
return new Date(year,month+num,day,hour,minute,second,milliSecond);
case "d":
return new Date(year,month,day+num,hour,minute,second,milliSecond);
case "ww":
return new Date(year+num,month,day+7*num,hour,minute,second,milliSecond);
case "h":
return new Date(year,month,day,hour+num,minute,second,milliSecond);
case "n":
return new Date(year,month,day,hour,minute+num,second,milliSecond);
case "s":
return new Date(year,month,day,hour,minute,second+num,milliSecond);
default:
return this;
}
};
/*****************************************************************
* 功能:把一个日期格式化成一个字符串
* 输入: formatString: 模板字符串%X为替换对象, Y,y Y表示4位年,y表示2位年, M,m表示月,M前不足2位前补0, D,d表示日,D不足2位前补0, H,h表示小时,H不足2位前补0, I,i表示分钟,S,s表示秒, R,r表示100000以内随机数, T表示时间戳,即Date的getTime函数返回的值
prefix前缀,postfix后缀
* 输出: 格式化后的字符串
******************************************************************/
Date.prototype.format = function(formatString,prefix,postfix) {
if(formatString) {
switch(formatString) {
case "date":
return this.format("%Y-%M-%D",prefix,postfix);
case "time":
return this.format("%H:%I:%S",prefix,postfix);
default:
var year = this.getFullYear();
var month = this.getMonth()+1;
var day = this.getDate();
var hour = this.getHours();
var minute = this.getMinutes();
var second = this.getSeconds();
var rand = Math.floor(Math.random()*100000).toString();
var t = this.getTime();
minute = minute<10?"0"+minute:minute;
second = second<10?"0"+second:second;
while(rand.length<5) { rand = "0"+rand;}
formatString = formatString.replace(/%Y/g,year);
formatString = formatString.replace(/%y/g,year.toString(10).substr(2,2));
formatString = formatString.replace(/%M/g,month<10?("0"+month):month);
formatString = formatString.replace(/%m/g,month).replace(/%D/g,day<10?("0"+day):day);
formatString = formatString.replace(/%d/g,day).replace(/%H/g,hour<10?("0"+hour):hour);
formatString = formatString.replace(/%h/ig,hour);
formatString = formatString.replace(/%i/ig,minute);
formatString = formatString.replace(/%s/ig,second);
formatString = formatString.replace(/%r/ig,rand);
formatString = formatString.replace(/%T/g,t);
return (prefix || "") +formatString+ (postfix || "");
}
} else {
return this.format("%Y-%M-%D %H:%I:%S",prefix,postfix);
}
}
/*****************************************************************
* 功能:获取一个时间和当前时间的差值
* 输入: flag: 时间差标识,endDate: 一个时间
* 输出: 差值
*****************************************************************/
Date.prototype.dateDiff = function(flag,endDate) {
var times = Date.parse(endDate) - Date.parse(this);
switch(format) {
case "y":
return endDate.getFullYear()-this.getFullYear();
break;
case "m":
return (endDate.getFullYear()-this.getFullYear())*12+endDate.getMonth()-this.getMonth();
break;
case "d":
return Math.ceil(times/(24*60*60*1000));
break;
case "h":
return Math.ceil(times/(60*60*1000));
break;
case "i":
return Math.ceil(times/(60*1000));
break;
case "s":
return Math.ceil(times/1000);
break;
}
};
/****************************************************************
* 功能:和Date.parse功能差不多,由于parse转出的没有毫秒,此函数中把毫秒添上,可以用于计算脚本执行时间
* 输入: 无
* 输出: 1970年午夜到当前时间的毫秒数
*****************************************************************/
Date.prototype.toUTC = function() {
return Date.parse(this)+this.getMilliseconds();
};
上面的几个函数只是简单的实现了,处理方法有的地方不够严谨,感兴趣的同学自行丰富
