大家可以看到js的date类的方法很少,而很少有人去扩展这个类,所以我就做了个扩展,供大家参考,欢迎大家补充。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>datetime</title>
</head>
<body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="javascript" type="text/javascript">![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.prototype.add = function(milliseconds)
{
var m = this.getTime() + milliseconds;
return new Date(m);
};
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.prototype.addSeconds = function(second)
{
return this.add(second * 1000);
};
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.prototype.addMinutes = function(minute)
{
return this.addSeconds(minute*60);
};
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.prototype.addHours = function(hour)
{
return this.addMinutes(60*hour);
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.prototype.addDays = function(day)
{
return this.addHours(day * 24);
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.isLeepYear = function(year)
{
return (year % 4 == 0 && year % 100 != 0)
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.daysInMonth = function(year,month)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(month == 2)
{
if(year % 4 == 0 && year % 100 != 0)
return 29;
else
return 28;
}
else if((month <= 7 && month % 2 == 1) || (month > 7 && month % 2 == 0))
return 31;
else
return 30;
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.prototype.addMonth = function()
{
var m = this.getMonth();
if(m == 11)return new Date(this.getFullYear() + 1,1,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
var daysInNextMonth = Date.daysInMonth(this.getFullYear(),this.getMonth() + 1);
var day = this.getDate();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(day > daysInNextMonth)
{
day = daysInNextMonth;
}
return new Date(this.getFullYear(),this.getMonth() + 1,day,this.getHours(),this.getMinutes(),this.getSeconds());
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.prototype.subMonth = function()
{
var m = this.getMonth();
if(m == 0)return new Date(this.getFullYear() -1,12,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
var day = this.getDate();
var daysInPreviousMonth = Date.daysInMonth(this.getFullYear(),this.getMonth());
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(day > daysInPreviousMonth)
{
day = daysInPreviousMonth;
}
return new Date(this.getFullYear(),this.getMonth() - 1,day,this.getHours(),this.getMinutes(),this.getSeconds());
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.prototype.addMonths = function(addMonth)
{
var result = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(addMonth > 0)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(addMonth > 0)
{
result = this.addMonth();
addMonth -- ;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if(addMonth < 0)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(addMonth < 0)
{
result = this.subMonth();
addMonth ++ ;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
result = this;
}
return result;
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Date.prototype.addYears = function(year)
{
return new Date(this.getFullYear() + year,this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var d = new Date();
alert('d.addYears(2) = ' + d.addYears(2).toLocaleString());
alert('d.addMonths(2) = ' + d.addMonths(2).toLocaleString());
alert('d.addMonths(-2) = ' + d.addMonths(-2).toLocaleString());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('d.addDays(2) = ' + d.addDays(2).toLocaleString());
alert('d.addHours(2) = ' + d.addHours(2).toLocaleString());
alert('d.addMinutes(2) = ' + d.addMinutes(2).toLocaleString());
alert('d.addSeconds(2) = ' + d.addSeconds(2).toLocaleString());
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
希望这个东西可以对大家有点用处。