javascript Date类的扩展

大家可以看到js的date类的方法很少,而很少有人去扩展这个类,所以我就做了个扩展,供大家参考,欢迎大家补充。

None.gif <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head >
None.gif    
< title > datetime </ title >
None.gif
</ head >
None.gif
< body >
None.gif
ExpandedBlockStart.gifContractedBlock.gif
< script  language ="javascript"  type ="text/javascript" > dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.prototype.add 
= function(milliseconds)dot.gif{
InBlock.gif    
var m = this.getTime() + milliseconds;
InBlock.gif    
return new Date(m);
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.prototype.addSeconds 
= function(second)dot.gif{
InBlock.gif    
return this.add(second * 1000);
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.prototype.addMinutes 
= function(minute)dot.gif{
InBlock.gif    
return this.addSeconds(minute*60);
ExpandedSubBlockEnd.gif}
;
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.prototype.addHours 
= function(hour)dot.gif{
InBlock.gif    
return this.addMinutes(60*hour);
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.prototype.addDays 
= function(day)dot.gif{
InBlock.gif    
return this.addHours(day * 24);
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.isLeepYear 
= function(year)dot.gif{
InBlock.gif    
return (year % 4 == 0 && year % 100 != 0)
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.daysInMonth 
= function(year,month)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(month == 2)dot.gif{
InBlock.gif        
if(year % 4 == 0 && year % 100 != 0)
InBlock.gif            
return 29;
InBlock.gif        
else
InBlock.gif            
return 28;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else if((month <= 7 && month % 2 == 1|| (month > 7 && month % 2 == 0))
InBlock.gif        
return 31;
InBlock.gif    
else
InBlock.gif        
return 30;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.prototype.addMonth 
= function()dot.gif{
InBlock.gif    
var m = this.getMonth();
InBlock.gif    
if(m == 11)return new Date(this.getFullYear() + 1,1,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
InBlock.gif    
InBlock.gif    
var daysInNextMonth = Date.daysInMonth(this.getFullYear(),this.getMonth() + 1);
InBlock.gif    
var day = this.getDate();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(day > daysInNextMonth)dot.gif{
InBlock.gif        day 
= daysInNextMonth;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return new Date(this.getFullYear(),this.getMonth() + 1,day,this.getHours(),this.getMinutes(),this.getSeconds());    
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.prototype.subMonth 
= function()dot.gif{
InBlock.gif    
var m = this.getMonth();
InBlock.gif    
if(m == 0)return new Date(this.getFullYear() -1,12,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
InBlock.gif    
var day = this.getDate();
InBlock.gif    
var daysInPreviousMonth = Date.daysInMonth(this.getFullYear(),this.getMonth());
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(day > daysInPreviousMonth)dot.gif{
InBlock.gif        day 
= daysInPreviousMonth;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return new Date(this.getFullYear(),this.getMonth() - 1,day,this.getHours(),this.getMinutes(),this.getSeconds());
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.prototype.addMonths 
= function(addMonth)dot.gif{
InBlock.gif    
var result = false;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(addMonth > 0)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while(addMonth > 0)dot.gif{
InBlock.gif            result 
= this.addMonth();
InBlock.gif            addMonth 
-- ;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif    }
else if(addMonth < 0)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while(addMonth < 0)dot.gif{
InBlock.gif            result 
= this.subMonth();
InBlock.gif            addMonth 
++ ;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif    }
elsedot.gif{
InBlock.gif        result 
= this;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return result;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifDate.prototype.addYears 
= function(year)dot.gif{
InBlock.gif    
return new Date(this.getFullYear() + year,this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
var d = new Date();
InBlock.gifalert('d.addYears(
2= ' + d.addYears(2).toLocaleString());
InBlock.gifalert('d.addMonths(
2= ' + d.addMonths(2).toLocaleString());
InBlock.gifalert('d.addMonths(
-2= ' + d.addMonths(-2).toLocaleString());
InBlock.gif
InBlock.gifalert('d.addDays(
2= ' + d.addDays(2).toLocaleString());
InBlock.gifalert('d.addHours(
2= ' + d.addHours(2).toLocaleString());
InBlock.gifalert('d.addMinutes(
2= ' + d.addMinutes(2).toLocaleString());
ExpandedBlockEnd.gifalert('d.addSeconds(
2= ' + d.addSeconds(2).toLocaleString());
None.gif
</ script >
None.gif
None.gif
</ body >
None.gif
</ html >
None.gif

希望这个东西可以对大家有点用处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值