大家可以看到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>


<script language="javascript" type="text/javascript">

Date.prototype.add = function(milliseconds)
{
var m = this.getTime() + milliseconds;
return new Date(m);
};

Date.prototype.addSeconds = function(second)
{
return this.add(second * 1000);
};

Date.prototype.addMinutes = function(minute)
{
return this.addSeconds(minute*60);
};

Date.prototype.addHours = function(hour)
{
return this.addMinutes(60*hour);
};


Date.prototype.addDays = function(day)
{
return this.addHours(day * 24);
};


Date.isLeepYear = function(year)
{
return (year % 4 == 0 && year % 100 != 0)
};


Date.daysInMonth = function(year,month)
{

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;
};


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();

if(day > daysInNextMonth)
{
day = daysInNextMonth;
}
return new Date(this.getFullYear(),this.getMonth() + 1,day,this.getHours(),this.getMinutes(),this.getSeconds());
};


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());

if(day > daysInPreviousMonth)
{
day = daysInPreviousMonth;
}
return new Date(this.getFullYear(),this.getMonth() - 1,day,this.getHours(),this.getMinutes(),this.getSeconds());
};


Date.prototype.addMonths = function(addMonth)
{
var result = false;

if(addMonth > 0)
{

while(addMonth > 0)
{
result = this.addMonth();
addMonth -- ;
}

}else if(addMonth < 0)
{

while(addMonth < 0)
{
result = this.subMonth();
addMonth ++ ;
}

}else
{
result = this;
}
return result;
};


Date.prototype.addYears = function(year)
{
return new Date(this.getFullYear() + year,this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());
};



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());

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>

</body>
</html>

希望这个东西可以对大家有点用处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架