javascript 日期时间函数(经典+完善+实用)
程序员文章站
2022-07-04 09:41:05
date.prototype.isleapyear 判断闰年 date.prototype.format 日期格式化 date.prototype.dateadd 日期计算...
date.prototype.isleapyear 判断闰年
date.prototype.format 日期格式化
date.prototype.dateadd 日期计算
date.prototype.datediff 比较日期差
date.prototype.tostring 日期转字符串
date.prototype.toarray 日期分割为数组
date.prototype.datepart 取日期的部分信息
date.prototype.maxdayofdate 取日期所在月的最大天数
date.prototype.weeknumofyear 判断日期所在年的第几周
stringtodate 字符串转日期型
isvaliddate 验证日期有效性
checkdatetime 完整日期时间检查
daysbetween 日期天数差
//---------------------------------------------------
// 判断闰年
//---------------------------------------------------
date.prototype.isleapyear = function()
{
return (0==this.getyear()%4&&((this.getyear()%100!=0)||(this.getyear()%400==0)));
}
//---------------------------------------------------
// 日期格式化
// 格式 yyyy/yyyy/yy/yy 表示年份
// mm/m 月份
// w/w 星期
// dd/dd/d/d 日期
// hh/hh/h/h 时间
// mm/m 分钟
// ss/ss/s/s 秒
//---------------------------------------------------
date.prototype.format = function(formatstr)
{
var str = formatstr;
var week = ['日','一','二','三','四','五','六'];
str=str.replace(/yyyy|yyyy/,this.getfullyear());
str=str.replace(/yy|yy/,(this.getyear() % 100)>9?(this.getyear() % 100).tostring():'0' + (this.getyear() % 100));
str=str.replace(/mm/,this.getmonth()>9?this.getmonth().tostring():'0' + this.getmonth());
str=str.replace(/m/g,this.getmonth());
str=str.replace(/w|w/g,week[this.getday()]);
str=str.replace(/dd|dd/,this.getdate()>9?this.getdate().tostring():'0' + this.getdate());
str=str.replace(/d|d/g,this.getdate());
str=str.replace(/hh|hh/,this.gethours()>9?this.gethours().tostring():'0' + this.gethours());
str=str.replace(/h|h/g,this.gethours());
str=str.replace(/mm/,this.getminutes()>9?this.getminutes().tostring():'0' + this.getminutes());
str=str.replace(/m/g,this.getminutes());
str=str.replace(/ss|ss/,this.getseconds()>9?this.getseconds().tostring():'0' + this.getseconds());
str=str.replace(/s|s/g,this.getseconds());
return str;
}
//+---------------------------------------------------
//| 求两个时间的天数差 日期格式为 yyyy-mm-dd
//+---------------------------------------------------
function daysbetween(dateone,datetwo)
{
var onemonth = dateone.substring(5,dateone.lastindexof ('-'));
var oneday = dateone.substring(dateone.length,dateone.lastindexof ('-')+1);
var oneyear = dateone.substring(0,dateone.indexof ('-'));
var twomonth = datetwo.substring(5,datetwo.lastindexof ('-'));
var twoday = datetwo.substring(datetwo.length,datetwo.lastindexof ('-')+1);
var twoyear = datetwo.substring(0,datetwo.indexof ('-'));
var cha=((date.parse(onemonth+'/'+oneday+'/'+oneyear)- date.parse(twomonth+'/'+twoday+'/'+twoyear))/86400000);
return math.abs(cha);
}
//+---------------------------------------------------
//| 日期计算
//+---------------------------------------------------
date.prototype.dateadd = function(strinterval, number) {
var dttmp = this;
switch (strinterval) {
case 's' :return new date(date.parse(dttmp) + (1000 * number));
case 'n' :return new date(date.parse(dttmp) + (60000 * number));
case 'h' :return new date(date.parse(dttmp) + (3600000 * number));
case 'd' :return new date(date.parse(dttmp) + (86400000 * number));
case 'w' :return new date(date.parse(dttmp) + ((86400000 * 7) * number));
case 'q' :return new date(dttmp.getfullyear(), (dttmp.getmonth()) + number*3, dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());
case 'm' :return new date(dttmp.getfullyear(), (dttmp.getmonth()) + number, dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());
case 'y' :return new date((dttmp.getfullyear() + number), dttmp.getmonth(), dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());
}
}
//+---------------------------------------------------
//| 比较日期差 dtend 格式为日期型或者 有效日期格式字符串
//+---------------------------------------------------
date.prototype.datediff = function(strinterval, dtend) {
var dtstart = this;
if (typeof dtend == 'string' )//如果是字符串转换为日期型
{
dtend = stringtodate(dtend);
}
switch (strinterval) {
case 's' :return parseint((dtend - dtstart) / 1000);
case 'n' :return parseint((dtend - dtstart) / 60000);
case 'h' :return parseint((dtend - dtstart) / 3600000);
case 'd' :return parseint((dtend - dtstart) / 86400000);
case 'w' :return parseint((dtend - dtstart) / (86400000 * 7));
case 'm' :return (dtend.getmonth()+1)+((dtend.getfullyear()-dtstart.getfullyear())*12) - (dtstart.getmonth()+1);
case 'y' :return dtend.getfullyear() - dtstart.getfullyear();
}
}
//+---------------------------------------------------
//| 日期输出字符串,重载了系统的tostring方法
//+---------------------------------------------------
date.prototype.tostring = function(showweek)
{
var mydate= this;
var str = mydate.tolocaledatestring();
if (showweek)
{
var week = ['日','一','二','三','四','五','六'];
str += ' 星期' + week[mydate.getday()];
}
return str;
}
//+---------------------------------------------------
//| 日期合法性验证
//| 格式为:yyyy-mm-dd或yyyy/mm/dd
//+---------------------------------------------------
function isvaliddate(datestr)
{
var sdate=datestr.replace(/(^\s+|\s+$)/g,''); //去两边空格;
if(sdate=='') return true;
//如果格式满足yyyy-(/)mm-(/)dd或yyyy-(/)m-(/)dd或yyyy-(/)m-(/)d或yyyy-(/)mm-(/)d就替换为''
//数据库中,合法日期可以是:yyyy-mm/dd(2003-3/21),数据库会自动转换为yyyy-mm-dd格式
var s = sdate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
if (s=='') //说明格式满足yyyy-mm-dd或yyyy-m-dd或yyyy-m-d或yyyy-mm-d
{
var t=new date(sdate.replace(/\-/g,'/'));
var ar = sdate.split(/[-/:]/);
if(ar[0] != t.getyear() || ar[1] != t.getmonth()+1 || ar[2] != t.getdate())
{
//alert('错误的日期格式!格式为:yyyy-mm-dd或yyyy/mm/dd。注意闰年。');
return false;
}
}
else
{
//alert('错误的日期格式!格式为:yyyy-mm-dd或yyyy/mm/dd。注意闰年。');
return false;
}
return true;
}
//+---------------------------------------------------
//| 日期时间检查
//| 格式为:yyyy-mm-dd hh:mm:ss
//+---------------------------------------------------
function checkdatetime(str)
{
var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
var r = str.match(reg);
if(r==null)return false;
r[2]=r[2]-1;
var d= new date(r[1],r[2],r[3],r[4],r[5],r[6]);
if(d.getfullyear()!=r[1])return false;
if(d.getmonth()!=r[2])return false;
if(d.getdate()!=r[3])return false;
if(d.gethours()!=r[4])return false;
if(d.getminutes()!=r[5])return false;
if(d.getseconds()!=r[6])return false;
return true;
}
//+---------------------------------------------------
//| 把日期分割成数组
//+---------------------------------------------------
date.prototype.toarray = function()
{
var mydate = this;
var myarray = array();
myarray[0] = mydate.getfullyear();
myarray[1] = mydate.getmonth();
myarray[2] = mydate.getdate();
myarray[3] = mydate.gethours();
myarray[4] = mydate.getminutes();
myarray[5] = mydate.getseconds();
return myarray;
}
//+---------------------------------------------------
//| 取得日期数据信息
//| 参数 interval 表示数据类型
//| y 年 m月 d日 w星期 ww周 h时 n分 s秒
//+---------------------------------------------------
date.prototype.datepart = function(interval)
{
var mydate = this;
var partstr='';
var week = ['日','一','二','三','四','五','六'];
switch (interval)
{
case 'y' :partstr = mydate.getfullyear();break;
case 'm' :partstr = mydate.getmonth()+1;break;
case 'd' :partstr = mydate.getdate();break;
case 'w' :partstr = week[mydate.getday()];break;
case 'ww' :partstr = mydate.weeknumofyear();break;
case 'h' :partstr = mydate.gethours();break;
case 'n' :partstr = mydate.getminutes();break;
case 's' :partstr = mydate.getseconds();break;
}
return partstr;
}
//+---------------------------------------------------
//| 取得当前日期所在月的最大天数
//+---------------------------------------------------
date.prototype.maxdayofdate = function()
{
var mydate = this;
var ary = mydate.toarray();
var date1 = (new date(ary[0],ary[1]+1,1));
var date2 = date1.dateadd(1,'m',1);
var result = datediff(date1.format('yyyy-mm-dd'),date2.format('yyyy-mm-dd'));
return result;
}
//+---------------------------------------------------
//| 取得当前日期所在周是一年中的第几周
//+---------------------------------------------------
date.prototype.weeknumofyear = function()
{
var mydate = this;
var ary = mydate.toarray();
var year = ary[0];
var month = ary[1]+1;
var day = ary[2];
document.write('< script language=vbscript\> \n');
document.write('mydate = datevalue(''+month+'-'+day+'-'+year+'') \n');
document.write('result = datepart('ww', mydate) \n');
document.write(' \n');
return result;
}
//+---------------------------------------------------
//| 字符串转成日期类型
//| 格式 mm/dd/yyyy mm-dd-yyyy yyyy/mm/dd yyyy-mm-dd
//+---------------------------------------------------
function stringtodate(datestr)
{
var converted = date.parse(datestr);
var mydate = new date(converted);
if (isnan(mydate))
{
//var delimcahar = datestr.indexof('/')!=-1?'/':'-';
var arys= datestr.split('-');
mydate = new date(arys[0],--arys[1],arys[2]);
}
return mydate;
}
date.prototype.format 日期格式化
date.prototype.dateadd 日期计算
date.prototype.datediff 比较日期差
date.prototype.tostring 日期转字符串
date.prototype.toarray 日期分割为数组
date.prototype.datepart 取日期的部分信息
date.prototype.maxdayofdate 取日期所在月的最大天数
date.prototype.weeknumofyear 判断日期所在年的第几周
stringtodate 字符串转日期型
isvaliddate 验证日期有效性
checkdatetime 完整日期时间检查
daysbetween 日期天数差
复制代码 代码如下:
//---------------------------------------------------
// 判断闰年
//---------------------------------------------------
date.prototype.isleapyear = function()
{
return (0==this.getyear()%4&&((this.getyear()%100!=0)||(this.getyear()%400==0)));
}
//---------------------------------------------------
// 日期格式化
// 格式 yyyy/yyyy/yy/yy 表示年份
// mm/m 月份
// w/w 星期
// dd/dd/d/d 日期
// hh/hh/h/h 时间
// mm/m 分钟
// ss/ss/s/s 秒
//---------------------------------------------------
date.prototype.format = function(formatstr)
{
var str = formatstr;
var week = ['日','一','二','三','四','五','六'];
str=str.replace(/yyyy|yyyy/,this.getfullyear());
str=str.replace(/yy|yy/,(this.getyear() % 100)>9?(this.getyear() % 100).tostring():'0' + (this.getyear() % 100));
str=str.replace(/mm/,this.getmonth()>9?this.getmonth().tostring():'0' + this.getmonth());
str=str.replace(/m/g,this.getmonth());
str=str.replace(/w|w/g,week[this.getday()]);
str=str.replace(/dd|dd/,this.getdate()>9?this.getdate().tostring():'0' + this.getdate());
str=str.replace(/d|d/g,this.getdate());
str=str.replace(/hh|hh/,this.gethours()>9?this.gethours().tostring():'0' + this.gethours());
str=str.replace(/h|h/g,this.gethours());
str=str.replace(/mm/,this.getminutes()>9?this.getminutes().tostring():'0' + this.getminutes());
str=str.replace(/m/g,this.getminutes());
str=str.replace(/ss|ss/,this.getseconds()>9?this.getseconds().tostring():'0' + this.getseconds());
str=str.replace(/s|s/g,this.getseconds());
return str;
}
//+---------------------------------------------------
//| 求两个时间的天数差 日期格式为 yyyy-mm-dd
//+---------------------------------------------------
function daysbetween(dateone,datetwo)
{
var onemonth = dateone.substring(5,dateone.lastindexof ('-'));
var oneday = dateone.substring(dateone.length,dateone.lastindexof ('-')+1);
var oneyear = dateone.substring(0,dateone.indexof ('-'));
var twomonth = datetwo.substring(5,datetwo.lastindexof ('-'));
var twoday = datetwo.substring(datetwo.length,datetwo.lastindexof ('-')+1);
var twoyear = datetwo.substring(0,datetwo.indexof ('-'));
var cha=((date.parse(onemonth+'/'+oneday+'/'+oneyear)- date.parse(twomonth+'/'+twoday+'/'+twoyear))/86400000);
return math.abs(cha);
}
//+---------------------------------------------------
//| 日期计算
//+---------------------------------------------------
date.prototype.dateadd = function(strinterval, number) {
var dttmp = this;
switch (strinterval) {
case 's' :return new date(date.parse(dttmp) + (1000 * number));
case 'n' :return new date(date.parse(dttmp) + (60000 * number));
case 'h' :return new date(date.parse(dttmp) + (3600000 * number));
case 'd' :return new date(date.parse(dttmp) + (86400000 * number));
case 'w' :return new date(date.parse(dttmp) + ((86400000 * 7) * number));
case 'q' :return new date(dttmp.getfullyear(), (dttmp.getmonth()) + number*3, dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());
case 'm' :return new date(dttmp.getfullyear(), (dttmp.getmonth()) + number, dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());
case 'y' :return new date((dttmp.getfullyear() + number), dttmp.getmonth(), dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());
}
}
//+---------------------------------------------------
//| 比较日期差 dtend 格式为日期型或者 有效日期格式字符串
//+---------------------------------------------------
date.prototype.datediff = function(strinterval, dtend) {
var dtstart = this;
if (typeof dtend == 'string' )//如果是字符串转换为日期型
{
dtend = stringtodate(dtend);
}
switch (strinterval) {
case 's' :return parseint((dtend - dtstart) / 1000);
case 'n' :return parseint((dtend - dtstart) / 60000);
case 'h' :return parseint((dtend - dtstart) / 3600000);
case 'd' :return parseint((dtend - dtstart) / 86400000);
case 'w' :return parseint((dtend - dtstart) / (86400000 * 7));
case 'm' :return (dtend.getmonth()+1)+((dtend.getfullyear()-dtstart.getfullyear())*12) - (dtstart.getmonth()+1);
case 'y' :return dtend.getfullyear() - dtstart.getfullyear();
}
}
//+---------------------------------------------------
//| 日期输出字符串,重载了系统的tostring方法
//+---------------------------------------------------
date.prototype.tostring = function(showweek)
{
var mydate= this;
var str = mydate.tolocaledatestring();
if (showweek)
{
var week = ['日','一','二','三','四','五','六'];
str += ' 星期' + week[mydate.getday()];
}
return str;
}
//+---------------------------------------------------
//| 日期合法性验证
//| 格式为:yyyy-mm-dd或yyyy/mm/dd
//+---------------------------------------------------
function isvaliddate(datestr)
{
var sdate=datestr.replace(/(^\s+|\s+$)/g,''); //去两边空格;
if(sdate=='') return true;
//如果格式满足yyyy-(/)mm-(/)dd或yyyy-(/)m-(/)dd或yyyy-(/)m-(/)d或yyyy-(/)mm-(/)d就替换为''
//数据库中,合法日期可以是:yyyy-mm/dd(2003-3/21),数据库会自动转换为yyyy-mm-dd格式
var s = sdate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
if (s=='') //说明格式满足yyyy-mm-dd或yyyy-m-dd或yyyy-m-d或yyyy-mm-d
{
var t=new date(sdate.replace(/\-/g,'/'));
var ar = sdate.split(/[-/:]/);
if(ar[0] != t.getyear() || ar[1] != t.getmonth()+1 || ar[2] != t.getdate())
{
//alert('错误的日期格式!格式为:yyyy-mm-dd或yyyy/mm/dd。注意闰年。');
return false;
}
}
else
{
//alert('错误的日期格式!格式为:yyyy-mm-dd或yyyy/mm/dd。注意闰年。');
return false;
}
return true;
}
//+---------------------------------------------------
//| 日期时间检查
//| 格式为:yyyy-mm-dd hh:mm:ss
//+---------------------------------------------------
function checkdatetime(str)
{
var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
var r = str.match(reg);
if(r==null)return false;
r[2]=r[2]-1;
var d= new date(r[1],r[2],r[3],r[4],r[5],r[6]);
if(d.getfullyear()!=r[1])return false;
if(d.getmonth()!=r[2])return false;
if(d.getdate()!=r[3])return false;
if(d.gethours()!=r[4])return false;
if(d.getminutes()!=r[5])return false;
if(d.getseconds()!=r[6])return false;
return true;
}
//+---------------------------------------------------
//| 把日期分割成数组
//+---------------------------------------------------
date.prototype.toarray = function()
{
var mydate = this;
var myarray = array();
myarray[0] = mydate.getfullyear();
myarray[1] = mydate.getmonth();
myarray[2] = mydate.getdate();
myarray[3] = mydate.gethours();
myarray[4] = mydate.getminutes();
myarray[5] = mydate.getseconds();
return myarray;
}
//+---------------------------------------------------
//| 取得日期数据信息
//| 参数 interval 表示数据类型
//| y 年 m月 d日 w星期 ww周 h时 n分 s秒
//+---------------------------------------------------
date.prototype.datepart = function(interval)
{
var mydate = this;
var partstr='';
var week = ['日','一','二','三','四','五','六'];
switch (interval)
{
case 'y' :partstr = mydate.getfullyear();break;
case 'm' :partstr = mydate.getmonth()+1;break;
case 'd' :partstr = mydate.getdate();break;
case 'w' :partstr = week[mydate.getday()];break;
case 'ww' :partstr = mydate.weeknumofyear();break;
case 'h' :partstr = mydate.gethours();break;
case 'n' :partstr = mydate.getminutes();break;
case 's' :partstr = mydate.getseconds();break;
}
return partstr;
}
//+---------------------------------------------------
//| 取得当前日期所在月的最大天数
//+---------------------------------------------------
date.prototype.maxdayofdate = function()
{
var mydate = this;
var ary = mydate.toarray();
var date1 = (new date(ary[0],ary[1]+1,1));
var date2 = date1.dateadd(1,'m',1);
var result = datediff(date1.format('yyyy-mm-dd'),date2.format('yyyy-mm-dd'));
return result;
}
//+---------------------------------------------------
//| 取得当前日期所在周是一年中的第几周
//+---------------------------------------------------
date.prototype.weeknumofyear = function()
{
var mydate = this;
var ary = mydate.toarray();
var year = ary[0];
var month = ary[1]+1;
var day = ary[2];
document.write('< script language=vbscript\> \n');
document.write('mydate = datevalue(''+month+'-'+day+'-'+year+'') \n');
document.write('result = datepart('ww', mydate) \n');
document.write(' \n');
return result;
}
//+---------------------------------------------------
//| 字符串转成日期类型
//| 格式 mm/dd/yyyy mm-dd-yyyy yyyy/mm/dd yyyy-mm-dd
//+---------------------------------------------------
function stringtodate(datestr)
{
var converted = date.parse(datestr);
var mydate = new date(converted);
if (isnan(mydate))
{
//var delimcahar = datestr.indexof('/')!=-1?'/':'-';
var arys= datestr.split('-');
mydate = new date(arys[0],--arys[1],arys[2]);
}
return mydate;
}