搜索
首页web前端js教程JS日期相加减函数整理

JS日期相加减函数整理

Dec 06, 2016 am 11:07 AM
js

// 增加天 
function AddDays(date,value) 

date.setDate(date.getDate()+value); 

// 增加月 
function AddMonths(date,value) 

date.setMonth(date.getMonth()+value); 

// 增加年 
function AddYears(date,value) 

date.setFullYear(date.getFullYear()+value); 

// 是否为今天 
function IsToday(date) 

return IsDateEquals(date,new Date()); 

// 是否为当月 
function IsThisMonth(date) 

return IsMonthEquals(date,new Date()); 

// 两个日期的年是否相等 
function IsMonthEquals(date1,date2) 

return date1.getMonth()==date2.getMonth()&&date1.getFullYear()==date2.getFullYear(); 

// 判断日期是否相等 
function IsDateEquals(date1,date2) 

return date1.getDate()==date2.getDate()&&IsMonthEquals(date1,date2); 

// 返回某个日期对应的月份的天数 
function GetMonthDayCount(date) 

switch(date.getMonth()+1) 

case 1:case 3:case 5:case 7:case 8:case 10:case 12: 
return 31; 
case 4:case 6:case 9:case 11: 
return 30; 

//feb: 
date=new Date(date); 
var lastd=28; 
date.setDate(29); 
while(date.getMonth()==1) 

lastd++; 
AddDays(date,1); 

return lastd; 

// 返回两位数的年份 
function GetHarfYear(date) 

var v=date.getYear(); 
if(v>9)return v.toString(); 
return "0"+v; 

// 返回月份(修正为两位数) 
function GetFullMonth(date) 

var v=date.getMonth()+1; 
if(v>9)return v.toString(); 
return "0"+v; 

// 返回日 (修正为两位数) 
function GetFullDate(date) 

var v=date.getDate(); 
if(v>9)return v.toString(); 
return "0"+v; 

// 替换字符串 
function Replace(str,from,to) 

return str.split(from).join(to); 

// 格式化日期的表示 
function FormatDate(date,str) 

str=Replace(str,"yyyy",date.getFullYear()); 
str=Replace(str,"MM",GetFullMonth(date)); 
str=Replace(str,"dd",GetFullDate(date)); 
str=Replace(str,"yy",GetHarfYear(date)); 
str=Replace(str,"M",date.getMonth()+1); 
str=Replace(str,"d",date.getDate()); 
return str; 

// 统一日期格式 
function ConvertDate(str) 

str=(str+"").replace(/^\s*/g,"").replace(/\s*$/g,""); // 去除前后的空白 
var d; 
if(/^[0-9]{8}$/.test(str)) // 20040226 -> 2004-02-26 

d=new Date(new Number(str.substr(0,4)),new Number(str.substr(4,2))-1,new Number(str.substr(6,2))); 
if(d.getTime())return d; 

d=new Date(str); 
if(d.getTime())return d; 
d=new Date(Replace(str,"-","/")); 
if(d.getTime())return d; 
return null; 
}  
js时间差函数 

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。