Javascript对日期的操作
详细请点击:http://www.verydemo.com/demo_c98_i60584.html
javascript对日期处理的常用方法类
<scriptlanguage="JavaScript"><br /><!--<br />//程序:常用公历日期处理程序<br />/**//*用相对不规则的字符串来创建日期对象,不规则的含义为:顺序包含年月日三个数值串,有间隔*/<br />String.prototype.parseDate=function(){<br /> varar=(this+",0,0,0").match(/d+/g);<br /> returnar[5]?(newDate(ar[0],ar[1]-1,ar[2],ar[3],ar[4],ar[5])):(newDate());<br />}<br />/**//*<br />*功能:根据输入表达式返回日期字符串<br />*参数:dateFmt:字符串,由以下结构组成 <br />* yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,<br />* mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.<br />* isFmtWithZero:是否用0进行格式化,trueorfalse<br />*返回:对应日期的中文字符串<br />*/<br />Date.prototype.toString=function(dateFmt,isFmtWithZero){<br /> dateFmt=(dateFmt==null?"yy-mm-ddhh:mi:ss":dateFmt);<br /> if(typeof(dateFmt)!="string")<br /> throw(newError(-1,'toString()方法的第一个参数为字符串类型!'));<br /> isFmtWithZero=!!isFmtWithZero;<br /> varweekArr=[["日","一","二","三","四","五","六"],["SUN","MON","TUR","WED","THU","FRI","SAT"]];<br /> varmonthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];<br /> varstr=dateFmt;<br /> varo={<br /> "yy":this.getFullYear(),<br /> "YY":this.getYear(),<br /> "mm":this.getMonth()+1,<br /> "MM":monthArr[this.getMonth()],<br /> "dd":this.getDate(),<br /> "hh":this.getHours(),<br /> "mi":this.getMinutes(),<br /> "ss":this.getSeconds(),<br /> "we":"星期"+weekArr[0][this.getDay()],<br /> "WE":weekArr[1][this.getDay()]<br /> }<br /> for(variino){<br /> str=str.replace(newRegExp(i,"g"),o[i].toString().fmtWithZero(isFmtWithZero));<br /> }<br /> str=str.replace(/ms/g,this.getMilliseconds().toString().fmtWithZeroD(isFmtWithZero));<br /> returnstr;<br />}<br />/**//*将一位数字格式化成两位,如:9to09*/<br />String.prototype.fmtWithZero=function(isFmtWithZero){ <br /> return(isFmtWithZero&&/^d$/.test(this))?"0"+this:this;<br />}<br />String.prototype.fmtWithZeroD=function(isFmtWithZero){ <br /> return(isFmtWithZero&&/^d{2}$/.test(this))?"00"+this:((isFmtWithZero&&/^d$/.test(this))?"0"+this:this);<br />}<br />/**//*功能:返回与某日期相距N天(N个24小时)的日期<br />*参数:numnumber类型可以为正负整数或者浮点数,默认为1;<br />* type0(秒)or1(天),默认为天<br />*返回:新的Date对象<br />*/<br />Date.prototype.dateAfter=function(num,type){<br /> num=(num==null?1:num);<br /> if(typeof(num)!="number")thrownewError(-1,"dateAfterDays(num,type)的num参数为数值类型.");<br /> type=(type==null?1:type);<br /> vararr=[1000,86400000];<br /> returnnewDate(this.valueOf()+num*arr[type]);<br />}<br />//判断是否是闰年,返回true或者false<br />Date.prototype.isLeapYear=function(){<br /> varyear=this.getFullYear();<br /> return(0==year%4&&((year%100!=0)||(year%400==0)));<br />}<br />//返回该月天数<br />Date.prototype.getDaysOfMonth=function(){<br /> return(newDate(this.getFullYear(),this.getMonth()+1,0)).getDate();<br />}<br />//日期比较函数,参数date:为Date类型,如this日期晚于参数:1,相等:0早于:-1<br />Date.prototype.dateCompare=function(date){<br /> if(typeof(date)!="object"||!(/Date/.test(date.constructor)))<br /> thrownewError(-1,"dateCompare(date)的date参数为Date类型.");<br /> vard=this.getTime()-date.getTime();<br /> returnd>0?1:(d==0?0:-1);<br />}<br />/**//*功能:返回两日期之差<br />*参数:pd PowerDate对象<br />* type:返回类别标识.yy:年,mm:月,ww:周,dd:日,hh:小时,mi:分,ss:秒,ms:毫秒<br />* intOrFloat:返回整型还是浮点型值0:整型,不等于0:浮点型<br />* output:输出提示,如:时间差为#周!<br />*/<br />Date.prototype.calDateDistance=function(date,type,intOrFloat,output){<br /> if(typeof(date)!="object"||!(/Date/.test(date.constructor)))<br /> thrownewError(-1,"calDateDistance(date,type,intOrFloat)的date参数为Date类型.");<br /> type=(type==null?'dd':type);<br /> if(!((newRegExp(type+",","g")).test("yy,mm,ww,dd,hh,mi,ss,ms,")))<br /> thrownewError(-1,"calDateDistance(pd,type,intOrFloat,output)的type参数为非法.");<br /> variof=(intOrFloat==null?0:intOrFloat);<br /> varnum=0;<br /> varo={<br /> "ww":7*86400000,<br /> "dd":86400000,<br /> "hh":3600000,<br /> "mi":60000,<br /> "ss":1000,<br /> "ms":1<br /> }<br /> switch(type){<br /> case"yy":num=this.getFullYear()-date.getFullYear();break;<br /> case"mm":num=(this.getFullYear()-date.getFullYear())*12+this.getMonth()-date.getMonth();break;<br /> default:<br /> varsub=this.valueOf()-date.valueOf();<br /> if(o[type])<br /> num=(sub/o[type]).fmtRtnVal(iof);<br /> break;<br /> }<br /> return(output?output.replace(/#/g,""+num+""):num);<br />}<br />//返回整数或者两位小数的浮点数<br />Number.prototype.fmtRtnVal=function(intOrFloat){<br /> return(intOrFloat==0?Math.floor(this):parseInt(this*100)/100);<br />}<br />//根据当前日期所在年和周数返回周日的日期<br />Date.prototype.RtnByWeekNum=function(weekNum){<br /> if(typeof(weekNum)!="number")<br /> thrownewError(-1,"RtnByWeekNum(weekNum)的参数是数字类型.");<br /> vardate=newDate(this.getFullYear(),0,1);<br /> varweek=date.getDay();<br /> week=(week==0?7:week);<br /> returndate.dateAfter(weekNum*7-week,1);<br />}<br />//根据日期返回该日期所在年的周数<br />Date.prototype.getWeekNum=function(){<br /> vardat=newDate(this.getFullYear(),0,1);<br /> varweek=dat.getDay();<br /> week=(week==0?7:week);<br /> vardays=this.calDateDistance(dat,"dd")+1;<br /> return((days+6-this.getDay()-7+week)/7);<br />}<br />//--><br /></script><br /><scriptlanguage="JavaScript"><br /><!--<br />vardw=document.write;<br />vardate2="2005-12-30".parseDate();<br />dw(date2,"<br>");<br />vardate3="2006-1-10111111".parseDate();<br />dw(date2.calDateDistance(date3,null,null,"时间差为#天!"));<br />//alert(newDate(2000,2,-29));<br />//--><br /></script>