// 增加天
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时间差函数
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号