简单谈谈javascript Date类型
1创建一个新的日期对象,如果不带参数,则对象自动获得当前的日期和时间
vard=newDate()
2如果需要指定特定的日期,则可以通过Date.parse()或者Date().UTC(),返回时间戳作为newDate()的参数
Date.parse()用法:
vartime=Date.parse('2015/05/20');
varnewDate=newDate(time);//WedMay20201500:00:00GMT+0800(中国标准时间)
//转换为格林威治时间
newDate.toUTCString();//Tue,19May201516:00:00GMT
也可以直接newDate('2015/05/20')指定日期,newDate()构造函数会自动调用Date.parse()静态方法。
Date.UTC()
Date.UTC()的参数分别是年,月(从0到11),日(1-31),时(0-23),分(0-59),秒(0-59),毫秒(0-999),最少参数2个,即应该包含年月,其他不填的默认为0。
如果要创建的时间为中国标准时间的2015年5月20日,则代码应表示为
varmyDate=newDate(Date.UTC(2015,5,19,16,0,0))//SatJun20201500:00:00GMT+0800 //格林威治时间 myDate.toUTCString()//Fri,19Jun201516:00:00GMT
其他:
vard=newDate(); //年 d.getFullYear() //月 d.getMonth() //日 d.getDate()
下面我们通过具体的示例来看看
/**
*日期时间脚本库方法列表:
*(1)Date.isValiDate:日期合法性验证
*(2)Date.isValiTime:时间合法性验证
*(3)Date.isValiDateTime:日期和时间合法性验证
*(4)Date.prototype.isLeapYear:判断是否闰年
*(5)Date.prototype.format:日期格式化
*(6)Date.stringToDate:字符串转成日期类型
*(7)Date.daysBetween:计算两个日期的天数差
*(8)Date.prototype.dateAdd:日期计算,支持正负数
*(9)Date.prototype.dateDiff:比较日期差:比较两个时期相同的字段,返回相差值
*(10)Date.prototype.toArray:把日期分割成数组:按数组序号分别为:年月日时分秒
*(11)Date.prototype.datePart:取得日期数据信息
*/
/**
*日期合法性验证:判断dataStr是否符合formatStr指定的日期格式
*示例:
*(1)alert(Date.isValiDate('2008-02-29','yyyy-MM-dd'));//true
*(2)alert(Date.isValiDate('aaaa-58-29','yyyy-MM-dd'));//false
*dateStr:必选,日期字符串
*formatStr:可选,格式字符串,可选格式有:(1)yyyy-MM-dd(默认格式)或YYYY-MM-DD(2)yyyy/MM/dd或YYYY/MM/DD(3)MM-dd-yyyy或MM-DD-YYYY(4)MM/dd/yyyy或MM/DD/YYYY
*/
Date.isValiDate=function(dateStr,formatStr)
{
if(!dateStr){
returnfalse;
}
if(!formatStr){
formatStr="yyyy-MM-dd";//默认格式:yyyy-MM-dd
}
if(dateStr.length!=formatStr.length){
returnfalse;
}else{
if(formatStr=="yyyy-MM-dd"||formatStr=="YYYY-MM-DD"){
varr1=/^(((((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26]))))\-((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-([0-2][0-9]))))|(\d{2}(([02468][1235679])|([13579][01345789]))\-((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-(([0-1][0-9])|(2[0-8]))))))$/;
returnr1.test(dateStr);
}elseif(formatStr=="yyyy/MM/dd"||formatStr=="YYYY/MM/DD"){
varr2=/^(((((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26]))))\/((((0[13578])|(1[02]))\/(([0-2][0-9])|(3[01])))|(((0[469])|(11))\/(([0-2][0-9])|(30)))|(02\/([0-2][0-9]))))|(\d{2}(([02468][1235679])|([13579][01345789]))\/((((0[13578])|(1[02]))\/(([0-2][0-9])|(3[01])))|(((0[469])|(11))\/(([0-2][0-9])|(30)))|(02\/(([0-1][0-9])|(2[0-8]))))))$/;
returnr2.test(dateStr);
}elseif(formatStr=="MM-dd-yyyy"||formatStr=="MM-DD-YYYY"){
varr3=/^((((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-([0-2][0-9])))\-(((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26])))))|(((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-(([0-1][0-9])|(2[0-8])))))\-\d{2}(([02468][1235679])|([13579][01345789])))$/;
returnr3.test(dateStr);
}elseif(formatStr=="MM/dd/yyyy"||formatStr=="MM/DD/YYYY"){
varr4=/^((((((0[13578])|(1[02]))\/(([0-2][0-9])|(3[01])))|(((0[469])|(11))\/(([0-2][0-9])|(30)))|(02\/([0-2][0-9])))\/(((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26])))))|(((((0[13578])|(1[02]))\/(([0-2][0-9])|(3[01])))|(((0[469])|(11))\/(([0-2][0-9])|(30)))|(02\/(([0-1][0-9])|(2[0-8])))))\/\d{2}(([02468][1235679])|([13579][01345789])))$/;
returnr4.test(dateStr);
}else{
alert("日期格式不正确!");
returnfalse;
}
}
returnfalse;
}
/**
*时间合法性验证:判断timeStr是否符合formatStr指定的时间格式
*示例:
*(1)alert(Date.isValiTime('23:59:59','hh:mm:ss'));//true
*(2)alert(Date.isValiTime('24-68-89','hh:mm:ss'));//false
*timeStr:必选,日期字符串
*formatStr:可选,格式字符串,可选格式有:(1)hh:mm:ss(默认格式)(2)hh-mm-ss(3)hh/mm/ss
*/
Date.isValiTime=function(timeStr,formatStr)
{
if(!timeStr){
returnfalse;
}
if(!formatStr){
formatStr="hh:mm:ss";//默认格式:hh:mm:ss
}
if(timeStr.length!=formatStr.length){
returnfalse;
}else{
if(formatStr=="hh:mm:ss"){
varr1=/^(([0-1][0-9])|(2[0-3]))\:([0-5][0-9])\:([0-5][0-9])$/;
returnr1.test(timeStr);
}elseif(formatStr=="hh-mm-ss"){
varr2=/^(([0-1][0-9])|(2[0-3]))\-([0-5][0-9])\-([0-5][0-9])$/;
returnr2.test(timeStr);
}elseif(formatStr=="hh/mm/ss"){
varr3=/^(([0-1][0-9])|(2[0-3]))\/([0-5][0-9])\/([0-5][0-9])$/;
returnr3.test(timeStr);
}else{
alert("时间格式不正确!");
returnfalse;
}
}
returnfalse;
}
/**
*日期和时间合法性验证
*格式:yyyy-MM-ddhh:mm:ss
*/
Date.isValiDateTime=function(dateTimeStr)
{
vardateTimeReg=/^(((((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26]))))\-((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-([0-2][0-9]))))|(\d{2}(([02468][1235679])|([13579][01345789]))\-((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-(([0-1][0-9])|(2[0-8]))))))(\s{1}(([0-1][0-9])|(2[0-3]))\:([0-5][0-9])\:([0-5][0-9]))?$/
returndateTimeReg.test(dateTimeStr);
}
/**
*判断闰年:一般规律为:四年一闰,百年不闰,四百年再闰。
*/
Date.prototype.isLeapYear=function()
{
return(this.getYear()%4==0&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
}
/**
*日期格式化:
*formatStr:可选,格式字符串,默认格式:yyyy-MM-ddhh:mm:ss
*约定如下格式:
*(1)YYYY/yyyy/YY/yy表示年份
*(2)MM/M月份
*(3)W/w星期
*(4)dd/DD/d/D日期
*(5)hh/HH/h/H时间
*(6)mm/m分钟
*(7)ss/SS/s/S秒
*(8)iii毫秒
*/
Date.prototype.format=function(formatStr)
{
varstr=formatStr;
if(!formatStr){
str="yyyy-MM-ddhh:mm:ss";//默认格式
}
varWeek=['日','一','二','三','四','五','六'];
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?(parseInt(this.getMonth())+1).toString():'0'+(parseInt(this.getMonth())+1));
str=str.replace(/M/g,(parseInt(this.getMonth())+1));
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());
str=str.replace(/iii/g,this.getMilliseconds()<10?'00'+this.getMilliseconds():(this.getMilliseconds()<100?'0'+this.getMilliseconds():this.getMilliseconds()));
returnstr;
}
/**
*字符串转成日期类型:
*dateStr:必选,日期字符串,如果无法解析成日期类型,返回null
*格式:
*(1)yyyy/MM/dd:IE和FF通用
*(2)MM/dd/yyyy:IE和FF通用
*(3)MM-dd-yyyy:仅IE
*(4)yyyy-MM-dd:非IE,且时钟被解析在8点整
*/
Date.stringToDate=function(dateStr)
{
if(!dateStr){
alert("字符串无法解析为日期");
returnnull;
}else{
if(Date.isValiDate(dateStr,"yyyy/MM/dd")||Date.isValiDate(dateStr,"MM/dd/yyyy")){
returnnewDate(Date.parse(dateStr));
}else{
if((!-[1,])){//IE
if(Date.isValiDate(dateStr,"MM-dd-yyyy")){
returnnewDate(Date.parse(dateStr));
}else{
alert("字符串无法解析为日期");
returnnull;
}
}else{//非IE
if(Date.isValiDate(dateStr,"yyyy-MM-dd")){
returnnewDate(Date.parse(dateStr));
}else{
alert("字符串无法解析为日期");
returnnull;
}
}
}
}
returnnull;
}
/**
*计算两个日期的天数差:
*dateOne:必选,必须是Data类型的实例
*dateTwo:必选,必须是Data类型的实例
*/
Date.daysBetween=function(dateOne,dateTwo)
{
if((dateOneinstanceofDate)==false||(dateTwoinstanceofDate)==false){
return0;
}else{
returnMath.abs(Math.floor((dateOne.getTime()-dateTwo.getTime())/1000/60/60/24));
}
}
/**
*日期计算:支持负数,即可加可减,返回计算后的日期
*num:必选,必须是数字,且正数是时期加,负数是日期减
*field:可选,标识是在哪个字段上进行相加或相减,字段见如下的约定。无此参数时,默认为d
*约定如下格式:
*(1)Y/y年
*(2)M月
*(3)W/w周
*(4)D/d日
*(5)H/h时
*(6)m分
*(7)S/s秒
*(8)Q/q季
*/
Date.prototype.dateAdd=function(num,field)
{
if((!num)||isNaN(num)||parseInt(num)==0){
returnthis;
}
if(!field){
field="d";
}
switch(field){
case'Y':
case'y':returnnewDate((this.getFullYear()+num),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());break;
case'Q':
case'q':returnnewDate(this.getFullYear(),(this.getMonth()+num*3),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());break;
case'M':returnnewDate(this.getFullYear(),this.getMonth()+num,this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds());break;
case'W':
case'w':returnnewDate(Date.parse(this)+((86400000*7)*num));break;
case'D':
case'd':returnnewDate(Date.parse(this)+(86400000*num));break;
case'H':
case'h':returnnewDate(Date.parse(this)+(3600000*num));break;
case'm':returnnewDate(Date.parse(this)+(60000*num));break;
case'S':
case's':returnnewDate(Date.parse(this)+(1000*num));break;
default:returnthis;
}
returnthis;
}
/**
*比较日期差:比较两个时期相同的字段,返回相差值
*dtEnd:必选,必须是Data类型的实例
*field:可选,标识是在哪个字段上进行比较,字段见如下的约定。无此参数时,默认为d
*约定如下格式:
*(1)Y/y年
*(2)M月
*(3)W/w周
*(4)D/d日
*(5)H/h时
*(6)m分
*(7)S/s秒
*/
Date.prototype.dateDiff=function(dtEnd,field)
{
vardtStart=this;
if((dtEndinstanceofDate)==false){
return0;
}else{
if(!field){
field="d";
}
switch(field){
case'Y':
case'y':returndtEnd.getFullYear()-dtStart.getFullYear();break;
case'M':return(dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12)-(dtStart.getMonth()+1);break;
case'W':
case'w':returnparseInt((dtEnd-dtStart)/(86400000*7));break;
case'D':
case'd':returnparseInt((dtEnd-dtStart)/86400000);break;
case'H':
case'h':returnparseInt((dtEnd-dtStart)/3600000);break;
case'm':returnparseInt((dtEnd-dtStart)/60000);break;
case'S':
case's':returnparseInt((dtEnd-dtStart)/1000);break;
default:return0;
}
return0;
}
}
/**
*把日期分割成数组:按数组序号分别为:年月日时分秒
*/
Date.prototype.toArray=function()
{
varmyArray=newArray();
myArray[0]=this.getFullYear();
myArray[1]=this.getMonth();
myArray[2]=this.getDate();
myArray[3]=this.getHours();
myArray[4]=this.getMinutes();
myArray[5]=this.getSeconds();
returnmyArray;
}
/**
*取得日期数据信息:
*field:可选,标识是在哪个字段上进行比较,字段见如下的约定。无此参数时,默认为d
*(1)Y/y年
*(2)M月
*(3)W/w周
*(4)D/d日
*(5)H/h时
*(6)m分
*(7)S/s秒
*/
Date.prototype.datePart=function(field)
{
if(!field){
field="d";
}
varWeek=['日','一','二','三','四','五','六'];
switch(field){
case'Y':
case'y':returnthis.getFullYear();break;
case'M':return(this.getMonth()+1);break;
case'W':
case'w':returnWeek[this.getDay()];break;
case'D':
case'd':returnthis.getDate();break;
case'H':
case'h':returnthis.getHours();break;
case'm':returnthis.getMinutes();break;
case's':returnthis.getSeconds();break;
default:returnthis.getDate();
}
returnthis.getDate();
}