[收藏]Javascript关于日期的各种技巧和方法总结[欢迎补充]

None.gif<script language="JavaScript">
None.gif
<!--
ExpandedBlockStart.gifContractedBlock.gif
function PowerDate(timeString)dot.gif{
InBlock.gif
this.date=null;
InBlock.gif
if(timeString!=""this.date=new Date(timeString);
InBlock.gif
else this.date=new Date();
InBlock.gif
this.isFmtZero=false;
InBlock.gif
this.weekArr=[["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
InBlock.gif  [
"SUN","MON","TUR","WED","THU","FRI","SAT"]];
InBlock.gif
this.monthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getFullYear=function()dot.gif{
InBlock.gif
return this.date.getFullYear();
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getYear=function()dot.gif{
InBlock.gif
return this.date.getYear();
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getMonth=function()dot.gif{
InBlock.gif
var mm=this.date.getMonth()+1;
InBlock.gif
if(this.isFmtZero==true && mm<10)
InBlock.gif
return "0"+mm;
InBlock.gif
else return mm;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getDay=function()dot.gif{
InBlock.gif
var dd=this.date.getDate();
InBlock.gif
if(this.isFmtZero==true && dd<10)
InBlock.gif
return "0"+dd;
InBlock.gif
else return dd;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getHour=function()dot.gif{
InBlock.gif
var hh=this.date.getHours();
InBlock.gif
if(this.isFmtZero==true && hh<10)
InBlock.gif
return "0"+hh;
InBlock.gif
else return hh;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getMinute=function()dot.gif{
InBlock.gif
var mi=this.date.getMinutes();
InBlock.gif
if(this.isFmtZero==true && mi<10)
InBlock.gif
return "0"+mi;
InBlock.gif
else return mi;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getSecond=function()dot.gif{
InBlock.gif
var ss=this.date.getSeconds();
InBlock.gif
if(this.isFmtZero==true && ss<10)
InBlock.gif
return "0"+ss;
InBlock.gif
else return ss;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getMillisecond=function()dot.gif{
InBlock.gif
var ss=this.date.getMilliseconds();
InBlock.gif
if(this.isFmtZero==true && ss<10)
InBlock.gif
return "00"+ss;
InBlock.gif
else if(this.isFmtZero==true && ss<100)
InBlock.gif
return "0"+ss;
InBlock.gif
else return ss;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getWeek=function()dot.gif{
InBlock.gif
return this.date.getDay();
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.setIsFmtZero=function(val)dot.gif{
InBlock.gif
this.isFmtZero=val;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*
InBlock.gif功能:根据输入表达式返回日期字符串
InBlock.gif参数:dateFmt:字符串,由以下结构组成 yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
InBlock.gifisFmtZero:布尔值,true:需要用0补位,false:不需要用0补位
ExpandedSubBlockEnd.gif
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getString=function(dateFmt)dot.gif{
InBlock.gif
if(typeof(dateFmt) != "string" )
InBlock.gif
throw(new Error(-1, 'getString()方法需要字符串类型参数!'));
InBlock.gif
var str=dateFmt;
InBlock.gifstr
=str.replace(/yy/g,this.getFullYear());
InBlock.gifstr
=str.replace(/YY/g,this.getYear());
InBlock.gifstr
=str.replace(/mm/g,this.getMonth());
InBlock.gifstr
=str.replace(/MM/g,this.monthArr[this.getMonth()-1]);
InBlock.gifstr
=str.replace(/dd/g,this.getDay());
InBlock.gifstr
=str.replace(/hh/g,this.getHour());
InBlock.gifstr
=str.replace(/mi/g,this.getMinute());
InBlock.gifstr
=str.replace(/ss/g,this.getSecond());
InBlock.gifstr
=str.replace(/ms/g,this.getMillisecond());
InBlock.gifstr
=str.replace(/we/g,this.weekArr[0][this.getWeek()]);
InBlock.gifstr
=str.replace(/WE/g,this.weekArr[1][this.getWeek()]);
InBlock.gif
return str;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
//返回N天前(后)的日期
InBlock.gif//
返回与另一日期之间的时间间隔
InBlock.gif//
ExpandedBlockEnd.gif
}

ExpandedBlockStart.gifContractedBlock.gifPowerDate.prototype.fmtWithZero
= function()dot.gif{
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
var d= new PowerDate("");
None.gifd.setIsFmtZero(
true);
None.gifalert(d.getString(
"yy-mm-dd hh:mi:ss.ms"));
None.gif
//-->
None.gif
</script>
None.gif
<body>
None.gif
</body>
None.gif



ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gif******************************************
InBlock.gif日期函数扩充 
InBlock.gif******************************************
ExpandedBlockEnd.gif
*/

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//转换成大写日期(中文)
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.toCase 
= function()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
var digits= new Array('零','一','二','三','四','五','六','七','八','九','十','十一','十二');
InBlock.gif
var unit= new Array('年','月','日','点','分','秒');
InBlock.gif
InBlock.gif
var year= this.getYear() + "";
InBlock.gif
var index;
InBlock.gif
var output="";
InBlock.gif
InBlock.gif
////////得到年
InBlock.gif
for (index=0;index<year.length;index++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifoutput 
+= digits[parseInt(year.substr(index,1))];
ExpandedSubBlockEnd.gif}

InBlock.gifoutput 
+=unit[0];
InBlock.gif
InBlock.gif
///////得到月
InBlock.gif
output +=digits[this.getMonth()] + unit[1];
InBlock.gif
InBlock.gif
///////得到日
InBlock.gif
switch (parseInt(this.getDate() / 10))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case 0:
InBlock.gifoutput 
+=digits[this.getDate() % 10];
InBlock.gif
break;
InBlock.gif
case 1:
InBlock.gifoutput 
+=digits[10+ ((this.getDate() % 10)>0?digits[(this.getDate() % 10)]:"");
InBlock.gif
break;
InBlock.gif
case 2:
InBlock.gif
case 3:
InBlock.gifoutput 
+=digits[parseInt(this.getDate() / 10)] + digits[10]  + ((this.getDate() % 10)>0?digits[(this.getDate() % 10)]:"");
InBlock.gif
default:
InBlock.gif
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gifoutput 
+=unit[2];
InBlock.gif
InBlock.gif
///////得到时
InBlock.gif
switch (parseInt(this.getHours() / 10))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case 0:
InBlock.gifoutput 
+=digits[this.getHours() % 10];
InBlock.gif
break;
InBlock.gif
case 1:
InBlock.gifoutput 
+=digits[10+ ((this.getHours() % 10)>0?digits[(this.getHours() % 10)]:"");
InBlock.gif
break;
InBlock.gif
case 2:
InBlock.gifoutput 
+=digits[parseInt(this.getHours() / 10)] + digits[10+ ((this.getHours() % 10)>0?digits[(this.getHours() % 10)]:"");
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gifoutput 
+=unit[3];
InBlock.gif
InBlock.gif
if(this.getMinutes()==0&&this.getSeconds()==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifoutput 
+="";
InBlock.gif
return output;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
///////得到分
InBlock.gif
switch (parseInt(this.getMinutes() / 10))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case 0:
InBlock.gifoutput 
+=digits[this.getMinutes() % 10];
InBlock.gif
break;
InBlock.gif
case 1:
InBlock.gifoutput 
+=digits[10+ ((this.getMinutes() % 10)>0?digits[(this.getMinutes() % 10)]:"");
InBlock.gif
break;
InBlock.gif
case 2:
InBlock.gif
case 3:
InBlock.gif
case 4:
InBlock.gif
case 5:
InBlock.gifoutput 
+=digits[parseInt(this.getMinutes() / 10)] + digits[10+ ((this.getMinutes() % 10)>0?digits[(this.getMinutes() % 10)]:"");
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gifoutput 
+=unit[4];
InBlock.gif
InBlock.gif
if(this.getSeconds()==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifoutput 
+="";
InBlock.gif
return output;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
///////得到秒
InBlock.gif
switch (parseInt(this.getSeconds() / 10))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case 0:
InBlock.gifoutput 
+=digits[this.getSeconds() % 10];
InBlock.gif
break;
InBlock.gif
case 1:
InBlock.gifoutput 
+=digits[10+ ((this.getSeconds() % 10)>0?digits[(this.getSeconds() % 10)]:"");
InBlock.gif
break;
InBlock.gif
case 2:
InBlock.gif
case 3:
InBlock.gif
case 4:
InBlock.gif
case 5:
InBlock.gifoutput 
+=digits[parseInt(this.getSeconds() / 10)] + digits[10+ ((this.getSeconds() % 10)>0?digits[(this.getSeconds() % 10)]:"");
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gifoutput 
+=unit[5];
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
return output;
ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//转换成农历
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.toChinese 
= function()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//暂缺
ExpandedBlockEnd.gif
}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//是否是闰年
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.isLeapYear 
= function()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//获得该月的天数
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.getDayCountInMonth 
= function()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
var mon = new Array(12);
InBlock.gif
InBlock.gif    mon[
0= 31; mon[1= 28; mon[2= 31; mon[3= 30; mon[4]  = 31; mon[5]  = 30;
InBlock.gif    mon[
6= 31; mon[7= 31; mon[8= 30; mon[9= 31; mon[10= 30; mon[11= 31;
InBlock.gif
InBlock.gif
if(0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0))&&this.getMonth()==2)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return 29;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return mon[this.getMonth()];
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//获得日期为星期几 (0为星期天)
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.weekOfDay 
= function()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
return this.getDay();
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//日期比较
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.Compare 
= function(objDate)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(typeof(objDate)!="object" && objDate.constructor != Date)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return -2;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
var d = this.getTime() - objDate.getTime();
InBlock.gif
InBlock.gif
if(d>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return 1;
ExpandedSubBlockEnd.gif}

InBlock.gif
else if(d==0
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return 0;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return -1;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif


ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gif===========================================
InBlock.gif//格式化日期格式
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.toString 
= function()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(arguments.length>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
var formatStr = arguments[0];
InBlock.gif
var str = formatStr;
InBlock.gif
InBlock.gifstr
=str.replace(/yyyy|YYYY/,this.getFullYear());
InBlock.gifstr
=str.replace(/yy|YY/,(this.getFullYear() % 100)>9?(this.getFullYear() % 100).toString():"0" + (this.getFullYear() % 100));
InBlock.gif
InBlock.gifstr
=str.replace(/MM/,this.getMonth()+1>9?(this.getMonth()+1).toString():"0" + (this.getMonth()+1));
InBlock.gifstr
=str.replace(/M/g,(this.getMonth()+1).toString());
InBlock.gif
InBlock.gifstr
=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():"0" + this.getDate());
InBlock.gifstr
=str.replace(/d|D/g,this.getDate());
InBlock.gif
InBlock.gifstr
=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():"0" + this.getHours());
InBlock.gifstr
=str.replace(/h|H/g,this.getHours());
InBlock.gif
InBlock.gifstr
=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():"0" + this.getMinutes());
InBlock.gifstr
=str.replace(/m/g,this.getMinutes());
InBlock.gif
InBlock.gifstr
=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():"0" + this.getSeconds());
InBlock.gifstr
=str.replace(/s|S/g,this.getSeconds());
InBlock.gif
InBlock.gif
return str;
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return this.toLocaleString();
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//由字符串直接实例日期对象
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.instanceFromString 
= function(str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
return new Date(str.replace(/-/g, "\/"));
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//得到日期年月日等加数字后的日期
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.dateAdd 
= function(interval,number)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
var date = this;
InBlock.gif
InBlock.gif    
switch(interval)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
case "y" : 
InBlock.gif            date.setFullYear(date.getFullYear()
+number);
InBlock.gif            
return date;
InBlock.gif
InBlock.gif        
case "q" : 
InBlock.gif            date.setMonth(date.getMonth()
+number*3);
InBlock.gif            
return date;
InBlock.gif
InBlock.gif        
case "m" : 
InBlock.gif            date.setMonth(date.getMonth()
+number);
InBlock.gif            
return date;
InBlock.gif
InBlock.gif        
case "w" : 
InBlock.gif            date.setDate(date.getDate()
+number*7);
InBlock.gif            
return date;
InBlock.gif        
InBlock.gif        
case "d" : 
InBlock.gif            date.setDate(date.getDate()
+number);
InBlock.gif            
return date;
InBlock.gif
InBlock.gif        
case "h" : 
InBlock.gif            date.setHours(date.getHours()
+number);
InBlock.gif            
return date;
InBlock.gif
InBlock.gif
case "m" : 
InBlock.gif            date.setMinutes(date.getMinutes()
+number);
InBlock.gif            
return date;
InBlock.gif
InBlock.gif
case "s" : 
InBlock.gif            date.setSeconds(date.getSeconds()
+number);
InBlock.gif            
return date;
InBlock.gif
InBlock.gif        
default : 
InBlock.gif            date.setDate(d.getDate()
+number);
InBlock.gif            
return date;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//计算两日期相差的日期年月日等
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.dateDiff 
= function(interval,o)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//判断o是否为日期对象
InBlock.gif
if(o&&o.constructor==Date)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//判断是否interval是否是字符串对象
InBlock.gif
if (interval&&interval.constructor==String) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
var _start= this.getTime();
InBlock.gif
var _end= o.getTime();
InBlock.gif
InBlock.gif
var number= _end - _start;
InBlock.gif
InBlock.gif
var iOut = -1;
InBlock.gif
InBlock.gif   
InBlock.gif
switch (interval.charAt(0))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case 'y':case 'Y'://year
InBlock.gif
iOut =  o.getFullYear() - this.getFullYear();
InBlock.gif
break;
InBlock.gif
case 'm':case 'M'://month
InBlock.gif
iOut = (o.getFullYear() - this.getFullYear()) * 12 + (o.getMonth()-this.getMonth());
InBlock.gif
break;
InBlock.gif
case 'q':case 'Q'://quarter
InBlock.gif
iOut = ((o.getFullYear() - this.getFullYear()) * 12 + (o.getMonth()-this.getMonth()))/3;
InBlock.gif
break;
InBlock.gif
case 'd':case 'D'://day
InBlock.gif
iOut = parseInt(number / 86400000) ;
InBlock.gif
break;
InBlock.gif
case 'w':case 'W'://week
InBlock.gif
iOut = parseInt(number / 86400000/7) ;
InBlock.gif
break;
InBlock.gif
case 'h':case 'H'://hour
InBlock.gif
iOut = parseInt(number / 3600000 ) ;
InBlock.gif
break;
InBlock.gif
case 'n':case 'N'://minute
InBlock.gif
iOut = parseInt(number / 60000 ) ;
InBlock.gif
break;
InBlock.gif
case 's': case 'S'://second
InBlock.gif
iOut = parseInt(number / 1000 ) ;
InBlock.gif
break;
InBlock.gif
case 't':case 'T'://microsecond
InBlock.gif
iOut = parseInt(number);
InBlock.gif
break;
InBlock.gif
default:
InBlock.gifiOut 
= -1;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return iOut;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return -1
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*
InBlock.gif===========================================
InBlock.gif//得到日期的组成部分
InBlock.gif===========================================
ExpandedBlockEnd.gif
*/

None.gifDate.prototype.datePart 
= function(interval)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
if(interval==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return -1;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(/^(yy|yyyy)$/.test(interval))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return this.getFullYear();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(/^(qq|q)$/.test(interval))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return Math.ceil((this.getMonth()+1/ 4);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(/^(mm|m)$/.test(interval))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return this.getMonth();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(/^(dd|d)$/.test(interval))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return this.getDate();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(/^(dw)$/.test(interval))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return this.getDay();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(/^(hh|h)$/.test(interval))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return this.getHours();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(/^(mi|n)$/.test(interval))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return this.getMinutes();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(/^(ss|s)$/.test(interval))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return this.getSeconds();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(/^(ms)$/.test(interval))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return this.getMilliseconds();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//缺少日期的第几周,第几天
InBlock.gif

InBlock.gif
return -1;
ExpandedBlockEnd.gif}

None.gif


None.gif**
None.gif
*功能:返回格式化后的日期字符串
None.gif
*参数:formatStr为格式字符串,其中表示形式如下列表
None.gif
*“ddd”-汉字星期几
None.gif
*“yyyy”-四位数年份
None.gif
*“MM”-两位数月份
None.gif
*“dd”-两位数日期
None.gif
*“hh”-两位数小时
None.gif
*“mm”-两位数分钟
None.gif
*“ss”-两位数秒数
None.gif
*“y”-年份
None.gif
*“M”-月份
None.gif
*“d”-日期
None.gif
*“h”-小时
None.gif
*“m”-分钟
None.gif
*“s”-秒数
None.gif
*/
ExpandedBlockStart.gifContractedBlock.gifDate.prototype.format 
= function (formatStr) dot.gif{
InBlock.gif
var WEEK = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
InBlock.gif
var s = formatStr;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/ddot.gif{3}/g, WEEK[this.getDay()]);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/ydot.gif{4}/g, this.getFullYear());
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/Mdot.gif{2}/g, (this.getMonth()+1)<10 ? "0"+(this.getMonth()+1) : (this.getMonth()+1));
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/ddot.gif{2}/g, this.getDate()<10 ? "0"+this.getDate() : this.getDate());
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/hdot.gif{2}/g, this.getHours()<10 ? "0"+this.getHours() : this.getHours());
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/mdot.gif{2}/g, this.getMinutes()<10 ? "0"+this.getMinutes() : this.getMinutes());
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/sdot.gif{2}/g, this.getSeconds()<10 ? "0"+this.getSeconds() : this.getSeconds());
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/ydot.gif{1}/g, this.getFullYear());
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/Mdot.gif{1}/g, this.getMonth() + 1);
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/ddot.gif{1}/g, this.getDate());
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/hdot.gif{1}/g, this.getHours());
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/mdot.gif{1}/g, this.getMinutes());
ExpandedSubBlockStart.gifContractedSubBlock.gif
= s.replace(/sdot.gif{1}/g, this.getSeconds());
InBlock.gif
InBlock.gif
return(s);
ExpandedBlockEnd.gif}

None.gif
None.gif


None.gif zhaoxiaoyang(梅雪香@深圳) 
None.gif整理完毕,发出来大家测测,有意见和建议快提,我好快改
None.gif
None.gif另:计算两个日期的差的功能,我不清楚该如何设计.请大家发表高论
!
None.gif
None.gif
<script language="JavaScript">
None.gif
<!--
ExpandedBlockStart.gifContractedBlock.gif
function PowerDate(timeString)dot.gif{
InBlock.gif
this.date=null;
InBlock.gif
if(timeString!=""this.date=new Date(timeString);
InBlock.gif
else this.date=new Date();
InBlock.gif
this.isFmtZero=false;
InBlock.gif
this.weekArr=[["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
InBlock.gif  [
"SUN","MON","TUR","WED","THU","FRI","SAT"]];
InBlock.gif
this.monthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getFullYear=function()dot.gif{
InBlock.gif
return this.date.getFullYear();
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getYear=function()dot.gif{
InBlock.gif
return this.date.getYear();
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getMonth=function()dot.gif{
InBlock.gif
var mm=this.date.getMonth()+1;
InBlock.gif
if(this.isFmtZero==true && mm<10)
InBlock.gif
return "0"+mm;
InBlock.gif
else return mm;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getDay=function()dot.gif{
InBlock.gif
var dd=this.date.getDate();
InBlock.gif
if(this.isFmtZero==true && dd<10)
InBlock.gif
return "0"+dd;
InBlock.gif
else return dd;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getHour=function()dot.gif{
InBlock.gif
var hh=this.date.getHours();
InBlock.gif
if(this.isFmtZero==true && hh<10)
InBlock.gif
return "0"+hh;
InBlock.gif
else return hh;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getMinute=function()dot.gif{
InBlock.gif
var mi=this.date.getMinutes();
InBlock.gif
if(this.isFmtZero==true && mi<10)
InBlock.gif
return "0"+mi;
InBlock.gif
else return mi;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getSecond=function()dot.gif{
InBlock.gif
var ss=this.date.getSeconds();
InBlock.gif
if(this.isFmtZero==true && ss<10)
InBlock.gif
return "0"+ss;
InBlock.gif
else return ss;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getMillisecond=function()dot.gif{
InBlock.gif
var ss=this.date.getMilliseconds();
InBlock.gif
if(this.isFmtZero==true && ss<10)
InBlock.gif
return "00"+ss;
InBlock.gif
else if(this.isFmtZero==true && ss<100)
InBlock.gif
return "0"+ss;
InBlock.gif
else return ss;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getWeek=function()dot.gif{
InBlock.gif
return this.date.getDay();
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.setIsFmtZero=function(val)dot.gif{
InBlock.gif
this.isFmtZero=val;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*
InBlock.gif功能:根据输入表达式返回日期字符串
InBlock.gif参数:dateFmt:字符串,由以下结构组成 yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
InBlock.gifisFmtZero:布尔值,true:需要用0补位,false:不需要用0补位
ExpandedSubBlockEnd.gif
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getString=function(dateFmt)dot.gif{
InBlock.gif
if(typeof(dateFmt) != "string" )
InBlock.gif
throw (new Error(-1, 'getString()方法需要字符串类型参数!'));
InBlock.gif
var str=dateFmt;
InBlock.gifstr
=str.replace(/yy/g,this.getFullYear());
InBlock.gifstr
=str.replace(/YY/g,this.getYear());
InBlock.gifstr
=str.replace(/mm/g,this.getMonth());
InBlock.gifstr
=str.replace(/MM/g,this.monthArr[this.getMonth()-1]);
InBlock.gifstr
=str.replace(/dd/g,this.getDay());
InBlock.gifstr
=str.replace(/hh/g,this.getHour());
InBlock.gifstr
=str.replace(/mi/g,this.getMinute());
InBlock.gifstr
=str.replace(/ss/g,this.getSecond());
InBlock.gifstr
=str.replace(/ms/g,this.getMillisecond());
InBlock.gifstr
=str.replace(/we/g,this.weekArr[0][this.getWeek()]);
InBlock.gifstr
=str.replace(/WE/g,this.weekArr[1][this.getWeek()]);
InBlock.gif
return str;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//* 名称 : dateAfterDays
InBlock.gif * 功能 : 返回与某日期相距N天(N个24小时)的日期
InBlock.gif * 参数 : num number类型 可以为正负整数或者浮点数
InBlock.gif * 返回 : 新的日期
InBlock.gif * 方法 : powerDate.dateAfterDays(num);
ExpandedSubBlockEnd.gif 
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
this.dateAfterDays=function(num)dot.gif{
InBlock.gif
if(typeof(num)!="number"throw new Error(-1,"dateAfterDays(num)参数为数值类型.");
InBlock.gif
var dd = this.date.valueOf();
InBlock.gifdd 
+= num*24*3600*1000;
InBlock.gif
this.date=new Date(dd);
InBlock.gif
return this;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//* 名称 : dateAfterSeconds
InBlock.gif * 功能 : 返回与某日期相距N秒的日期
InBlock.gif * 参数 : num number类型 可以为正负整数或者浮点数
InBlock.gif * 返回 : 新的日期
InBlock.gif * 方法 : powerDate.dateAfterDays(num);
ExpandedSubBlockEnd.gif 
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
this.dateAfterSeconds=function(num)dot.gif{
InBlock.gif
if(typeof(num)!="number"throw new Error(-1,"dateAfterDays(num)参数为数值类型.");
InBlock.gif
var dd = this.date.valueOf();
InBlock.gifdd 
+= num*1000;
InBlock.gif
this.date=new Date(dd);
InBlock.gif
return this;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//判断是否是闰年
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.isLeapYear = function ()dot.gif{
InBlock.gif
var year = this.getFullYear();
InBlock.gif
return (0==year%4 && ((year % 100 != 0)||(year % 400 == 0)));
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//返回该月天数
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getDaysOfMonth = function ()dot.gif{
InBlock.gif
return (new Date(this.getFullYear(),this.getMonth(),0)).getDate();
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//转换成大写日期(中文)
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getChinaDate =  function()dot.gif{
InBlock.gif
var year = this.getFullYear().toString();
InBlock.gif
var month= this.getMonth().toString();
InBlock.gif
var day = this.getDay().toString();
InBlock.gif
var arrNum = ["","","","","","","","","","","","十一","十二"];
InBlock.gif
var strTmp="";
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(var i=0,j=year.length;i<j;i++)dot.gif{
InBlock.gifstrTmp 
+= arrNum[year.charAt(i)];
ExpandedSubBlockEnd.gif}

InBlock.gifstrTmp 
+= "";
InBlock.gifstrTmp 
+= arrNum[month]+"";
InBlock.gif
if(day<10)
InBlock.gifstrTmp 
+= arrNum[day];
InBlock.gif
else if (day <20)
InBlock.gifstrTmp 
+= ""+arrNum[day-10];
InBlock.gif
else if (day <30 )
InBlock.gifstrTmp 
+= "二十"+arrNum[day-20];
InBlock.gif
else 
InBlock.gifstrTmp 
+= "三十"+arrNum[day-30];
InBlock.gifstrTmp 
+= "";
InBlock.gif
return strTmp;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//日期比较函数,如大于参数:1,相等:0 不等: -1
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.dateCompare = function(dat)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(typeof(dat)=="string")dot.gif{
InBlock.gif
if(dat!="") dat=new Date(timeString);
InBlock.gif
else dat=new Date();
ExpandedSubBlockEnd.gif}

InBlock.gif
if(typeof(dat)!="object" || dat.constructor != Date)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Error(-2,"dateCompare的参数为日期类型或者可直接转化为日期类型的字符串!");
ExpandedSubBlockEnd.gif}

InBlock.gif
var d = this.date.getTime() - dat.getTime();
InBlock.gif
return d>0?1:(d==0?0:-1);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
var d= new PowerDate("");
None.gifd.setIsFmtZero(
true);
None.gif
None.gifalert(d.getString(
"yy-mm-dd hh:mi:ss.ms"));
None.gif
None.gif
//-->
None.gif
</script>
None.gif
<body>
None.gif
</body>
None.gif
None.gif
None.gif
None.gif又改写了一下,可以用所有初始化Date对象的方法来初始化PowerDate对象了,而且可以是更不符合要求的字符串如: 
new PowerDate("  2005@#@#8----22    ");也一样可以成功初始化,只是一定要包含三个数值.
None.gif农历的算法太长了,而且在开发中也很少应用,所以暂不加到该类中来.
None.gif新代码如下:
None.gif
None.gif
<script language="JavaScript">
None.gif
<!--
ExpandedBlockStart.gifContractedBlock.gif
function PowerDate()dot.gif{
InBlock.gif
//日期对象
InBlock.gif
this.date=null;
InBlock.gif
//格式化时是否加零补位标志
InBlock.gif
this.isFmtZero=false;
InBlock.gif
this.weekArr=[["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
InBlock.gif  [
"SUN","MON","TUR","WED","THU","FRI","SAT"]];
InBlock.gif
this.monthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
InBlock.gif
//初始化日期对象
ExpandedSubBlockStart.gifContractedSubBlock.gif
switch(arguments.length)dot.gif{
InBlock.gif
case 0:
InBlock.gif
this.date = new Date();
InBlock.gif
break;
InBlock.gif
case 1
ExpandedSubBlockStart.gifContractedSubBlock.gif
var reg = /^(\ddot.gif{2,4})\D+(\ddot.gif{1,2})\D+(\ddot.gif{1,2})$/;
InBlock.gif
var str = arguments[0].replace(/\s/,"");
InBlock.gifstr 
= str.replace(reg,"$1/$2/$3");
InBlock.gif
this.date = new Date(str);
InBlock.gif
break;
InBlock.gif
case 3
InBlock.gif
this.date = new Date(arguments[0],arguments[1]-1,arguments[2]);
InBlock.gif
break;
InBlock.gif
case 6
InBlock.gif
this.date = new Date(arguments[0],arguments[1]-1,arguments[2],arguments[3],arguments[4],arguments[5]);
InBlock.gif
break;
InBlock.gif
case 7
InBlock.gif
this.date = new Date(arguments[0],arguments[1]-1,arguments[2],arguments[3],arguments[4],arguments[5],arguments[6]);
InBlock.gif
break;
InBlock.gif
defaultthis.date = new Date("1970/1/1"); break;
ExpandedSubBlockEnd.gif}

InBlock.gif
//初始化失败处理
InBlock.gif
if(typeof(this.date) != "object" || !(/Date/.test(this.date.constructor)))
InBlock.gif
throw (new Error(-1, '构造PowerDate方法失败,检查输入参数!'));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getDate = function ()dot.gif{
InBlock.gif
return this.date;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getFullYear=function()dot.gif{
InBlock.gif
return this.date.getFullYear();
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getYear=function()dot.gif{
InBlock.gif
return this.date.getYear();
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getMonth=function()dot.gif{
InBlock.gif
return this.frmWithZero(this.date.getMonth()+1);
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getDay=function()dot.gif{
InBlock.gif
return this.frmWithZero(this.date.getDate());
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getHour=function()dot.gif{
InBlock.gif
return this.frmWithZero(this.date.getHours());
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getMinute=function()dot.gif{
InBlock.gif
return this.frmWithZero(this.date.getMinutes());
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getSecond=function()dot.gif{
InBlock.gif
return this.frmWithZero(this.date.getSeconds());
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getMillisecond=function()dot.gif{
InBlock.gif
var ss=this.date.getMilliseconds();
InBlock.gif
if(this.isFmtZero==true && ss<10)
InBlock.gif
return "00"+ss;
InBlock.gif
else if(this.isFmtZero==true && ss<100)
InBlock.gif
return "0"+ss;
InBlock.gif
else return ss;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getWeek=function()dot.gif{
InBlock.gif
return this.date.getDay();
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.setIsFmtZero=function(val)dot.gif{
InBlock.gif
this.isFmtZero=val;
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.frmWithZero = function(num)dot.gif{
InBlock.gif
if(this.isFmtZero==true && num<10)
InBlock.gif
return "0"+num;
InBlock.gif
else return num;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*
InBlock.gif功能:根据输入表达式返回日期字符串
InBlock.gif参数:dateFmt:字符串,由以下结构组成 yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
InBlock.gifisFmtZero:布尔值,true:需要用0补位,false:不需要用0补位
ExpandedSubBlockEnd.gif
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getString=function(dateFmt)dot.gif{
InBlock.gif
if(typeof(dateFmt) != "string" )
InBlock.gif
throw (new Error(-1, 'getString()方法需要字符串类型参数!'));
InBlock.gif
var str=dateFmt;
InBlock.gifstr
=str.replace(/yy/g,this.getFullYear());
InBlock.gifstr
=str.replace(/YY/g,this.getYear());
InBlock.gifstr
=str.replace(/mm/g,this.getMonth());
InBlock.gifstr
=str.replace(/MM/g,this.monthArr[this.getMonth()-1]);
InBlock.gifstr
=str.replace(/dd/g,this.getDay());
InBlock.gifstr
=str.replace(/hh/g,this.getHour());
InBlock.gifstr
=str.replace(/mi/g,this.getMinute());
InBlock.gifstr
=str.replace(/ss/g,this.getSecond());
InBlock.gifstr
=str.replace(/ms/g,this.getMillisecond());
InBlock.gifstr
=str.replace(/we/g,this.weekArr[0][this.getWeek()]);
InBlock.gifstr
=str.replace(/WE/g,this.weekArr[1][this.getWeek()]);
InBlock.gif
return str;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//* 功能 : 返回与某日期相距N天(N个24小时)的日期
InBlock.gif * 参数 : num number类型 可以为正负整数或者浮点数
InBlock.gif * 返回 : 新的PowerDate类型
InBlock.gif * 方法 : powerDate.dateAfterDays(num);
ExpandedSubBlockEnd.gif 
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
this.dateAfterDays=function(num)dot.gif{
InBlock.gif
if(typeof(num)!="number"throw new Error(-1,"dateAfterDays(num)参数为数值类型.");
InBlock.gif
var dd = this.date.valueOf();
InBlock.gifdd 
+= num*24*3600*1000;
InBlock.gif
this.date=new Date(dd);
InBlock.gif
return this;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//* 功能 : 返回与某日期相距N秒的日期
InBlock.gif * 参数 : num number类型 可以为正负整数或者浮点数
InBlock.gif * 返回 : 新的日期
InBlock.gif * 方法 : powerDate.dateAfterDays(num);
ExpandedSubBlockEnd.gif 
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
this.dateAfterSeconds=function(num)dot.gif{
InBlock.gif
if(typeof(num)!="number"throw new Error(-1,"dateAfterDays(num)参数为数值类型.");
InBlock.gif
var dd = this.date.valueOf();
InBlock.gifdd 
+= num*1000;
InBlock.gif
this.date=new Date(dd);
InBlock.gif
return this;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//判断是否是闰年
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.isLeapYear = function ()dot.gif{
InBlock.gif
var year = this.getFullYear();
InBlock.gif
return (0==year%4 && ((year % 100 != 0)||(year % 400 == 0)));
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//返回该月天数
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getDaysOfMonth = function ()dot.gif{
InBlock.gif
return (new Date(this.getFullYear(),this.getMonth(),0)).getDate();
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//转换成大写日期(中文)
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.getChinaDate =  function()dot.gif{
InBlock.gif
var year = this.getFullYear();
InBlock.gif
var month= this.getMonth();
InBlock.gif
var day = this.getDay();
InBlock.gif
var arrNum = ["","","","","","","","","","","","十一","十二"];
InBlock.gif
var strTmp="";
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(var i=0,j=year.length;i<j;i++)dot.gif{
InBlock.gifstrTmp 
+= arrNum[year.charAt(i)];
ExpandedSubBlockEnd.gif}

InBlock.gifstrTmp 
+= "";
InBlock.gifstrTmp 
+= arrNum[month]+"";
InBlock.gif
if(day<10)
InBlock.gifstrTmp 
+= arrNum[day];
InBlock.gif
else if (day <20)
InBlock.gifstrTmp 
+= ""+arrNum[day-10];
InBlock.gif
else if (day <30 )
InBlock.gifstrTmp 
+= "二十"+arrNum[day-20];
InBlock.gif
else 
InBlock.gifstrTmp 
+= "三十"+arrNum[day-30];
InBlock.gifstrTmp 
+= "";
InBlock.gif
return strTmp;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
//日期比较函数,如大于参数:1,相等:0 不等: -1
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.dateCompare = function(dat)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(typeof(dat)=="string")dot.gif{
InBlock.gif
if(dat!="") dat=new Date(timeString);
InBlock.gif
else dat=new Date();
ExpandedSubBlockEnd.gif}

InBlock.gif
if(typeof(dat)!="object" || !(/Date/.test(this.date.constructor)))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Error(-2,"dateCompare的参数为日期类型或者可直接转化为日期类型的字符串!");
ExpandedSubBlockEnd.gif}

InBlock.gif
var d = this.date.getTime() - dat.getTime();
InBlock.gif
return d>0?1:(d==0?0:-1);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
var d= new PowerDate(" 98-5/1 ");//实例化一个PowerDate对象
None.gif
d.setIsFmtZero(true);//设置为用0补位输出
None.gif
alert(d.getString("yy-mm-dd hh:mi:ss.ms"));//输出日期字符串
None.gif

None.gif
//-->
None.gif
</script>
None.gif
None.gif
None.gif新加了记算日期的方法,请大家帮测一下
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//*功能:返回两日期之差
InBlock.gif *参数:pd   PowerDate对象
InBlock.gif *    type: 返回类别标识.yy:年,mm:月,dd:日,hh:小时,mi:分,ss:秒,ms:毫秒
InBlock.gif *    intOrFloat :返回整型还是浮点型值 0:整型,1:浮点型
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
this.calDateDistance = function (pd,type,intOrFloat)dot.gif{
InBlock.gif
var miSecMain = this.date.valueOf();
InBlock.gif
var miSecSub  = pd.getDate().valueOf();
InBlock.gif
var num=0;
ExpandedSubBlockStart.gifContractedSubBlock.gif
switch(type)dot.gif{
InBlock.gif
case "yy": num = this.getFullYear() - pd.getFullYear();
InBlock.gif
break;
InBlock.gif
case "mm": num = (this.getFullYear() - pd.getFullYear())*12+this.getMonth()-pd.getMonth();
InBlock.gif
break;
InBlock.gif
case "dd": num = this.fmtRtnVal((miSecMain-miSecSub)/86400000,intOrFloat);
InBlock.gif
break;
InBlock.gif
case "hh": num = this.fmtRtnVal((miSecMain-miSecSub)/3600000,intOrFloat);
InBlock.gif
break;
InBlock.gif
case "mi": num = this.fmtRtnVal((miSecMain-miSecSub)/60000,intOrFloat);
InBlock.gif
break;
InBlock.gif
case "ss": num = this.fmtRtnVal((miSecMain-miSecSub)/1000,intOrFloat);
InBlock.gif
break;
InBlock.gif
case "ms": num = (miSecMain-miSecSub);
InBlock.gif
break;
InBlock.gif
default:
InBlock.gif
throw new Error(-1,"没有您要求返回的类型,请检查输入参数!");
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gif
return num;
ExpandedBlockEnd.gif}
;
ExpandedBlockStart.gifContractedBlock.gif
this.fmtRtnVal = function (val,intOrFloat)dot.gif{
InBlock.gif
//alert(val);
InBlock.gif
return (intOrFloat == 0 ? Math.floor(val) : parseInt(val*100)/100);
ExpandedBlockEnd.gif}
;
None.gif
None.gif
None.gif
None.gif测试语句:
None.gif
None.gif
var d= new PowerDate(new Date());//实例化一个PowerDate对象
None.gif
alert(d.calDateDistance(new  PowerDate("2005/5/31"),"hh",1));//输出日期字符串
None.gif

None.gif
None.gif
None.gif

转载于:https://www.cnblogs.com/goody9807/archive/2005/11/24/283495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值