日期:2014-05-16  浏览次数:20371 次

一个日期的扩展函数库包括计算周次和本周时间范围(javascript)
< type="text/javascript"> //
/*用相对不规则的字符串来创建日期对象,不规则的含义为:顺序包含年月日三个数值串,有间隔*/
String.prototype.createDate = function(){
var regThree = /^\D*(\d{2,4})\D+(\d{1,2})\D+(\d{1,2})\D*$/;
var regSix = /^\D*(\d{2,4})\D+(\d{1,2})\D+(\d{1,2})\D+(\d{1,2})\D+(\d{1,2})\D+(\d{1,2})\D*$/;
var str = "";
var date = null;
if(regThree.test(this)){
str = this.replace(/\s/g,"").replace(regThree,"$1/$2/$3");
date = new Date(str);
}
else if(regSix.test(this)){
str = this.match(regSix);
date = new Date(str[1],str[2]-1,str[3],str[4],str[5],str[6]);
}
if(isNaN(date))return new Date();
else return date;
}

/*
* 功能:根据输入表达式返回日期字符串
* 参数:dateFmt:字符串,由以下结构组成
* yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,
* mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
* isFmtWithZero : 是否用0进行格式化,true or false
*/
Date.prototype.parseString = function(dateFmt,isFmtWithZero){
dateFmt = (dateFmt == null?"yy-mm-dd" : dateFmt);
isFmtWithZero = (isFmtWithZero == null?true : isFmtWithZero);
if(typeof(dateFmt) != "string" )
throw (new Error(-1, 'parseString()方法需要字符串类型参数!'));
var weekArr=[["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
["SUN","MON","TUR","WED","THU","FRI","SAT"]];
var monthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var str=dateFmt;
str = str.replace(/yy/g,this.getFullYear());
str = str.replace(/YY/g,this.getYear());
str = str.replace(/mm/g,(this.getMonth()+1).toString().fmtWithZero(isFmtWithZero));
str = str.replace(/MM/g,monthArr[this.getMonth()]);
str = str.replace(/dd/g,this.getDate().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/hh/g,this.getHours().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/mi/g,this.getMinutes().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/ss/g,this.getSeconds().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/ms/g,this.getMilliseconds().toString().fmtWithZeroD(isFmtWithZero));
str = str.replace(/we/g,weekArr[0][this.getDay()]);
str = str.replace(/WE/g,weekArr[1][this.getDay()]);
return str;
}

/*将一位数字格式化成两位,如: 9 to 09*/
String.prototype.fmtWithZero = function(isFmtWithZero){
if(isFmtWithZero)
return (this<10?"0"+this:this);
else return this;
}
String.prototype.fmtWithZeroD = function(isFmtWithZero){
if(isFmtWithZero)
return (this<10?"00"+this:(this<100?"0"+this:this));
else return this;
}

/* 功能 : 返回与某日期相距N天(N个24小时)的日期
* 参数 : num number类型 可以为正负整数或者浮点数,默认为1;
* type 0(秒) or 1(天),默认为秒
* 返回 : 新的PowerDate类型
*/
Date.prototype.dateAfter=function(num,type){
num = (num == null?1:num);
if(typeof(num)!="number") throw new Error(-1,"dateAfterDays(num,type)的num参数为数值类型.");
type = (type==null?0:type);
var arr = [1000,86400000];
var dd = this.valueOf();
dd += num*arr[type];
return new Date(dd);
}

//判断是否是闰年,返回true 或者 false
Date.prototype.isLeapYear = function (){
var year = this.getFullYear();
return (0==year%4 && ((year % 100 != 0)||(year % 400 == 0)));
}


//返回该月天数
Date.prototype.getDaysOfMonth = function (){
return (new Date(this.getFullYear(),this.getMonth()+1,0)).getDate();
}

//转换成大写日期(中文)
Date.prototype.getChinaDate = function(){
var year = this.getFullYear().toString();
var month= this.getMonth()+1;
var day = this.getDate();
var arrNum = ["零","一","二","三","四","五","六","七","八","九","十","十一","十二"];
var strTmp="";
for(var i=0,j=year.length;i<j;i++){
strTmp += arrNum[year.charAt(i)];
}
strTmp += "年";
strTmp += arrNum[month]+"月";
if(day<10)
strTmp += arrNum[day];
else if (day <20)
strTmp += "十"+arrNum[day-10];
e