簡(jiǎn)單談?wù)刯avascript Date類型
1 創(chuàng)建一個(gè)新的日期對(duì)象,如果不帶參數(shù),則對(duì)象自動(dòng)獲得當(dāng)前的日期和時(shí)間
var d = new Date()
2 如果需要指定特定的日期,則可以通過Date.parse() 或者 Date().UTC(),返回時(shí)間戳作為 new Date()的參數(shù)
Date.parse() 用法:
var time = Date.parse('2015/05/20'); var newDate = new Date(time);//Wed May 20 2015 00:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間) //轉(zhuǎn)換為格林威治時(shí)間 newDate.toUTCString(); //Tue, 19 May 2015 16:00:00 GMT
也可以直接 new Date('2015/05/20') 指定日期,new Date() 構(gòu)造函數(shù)會(huì)自動(dòng)調(diào)用 Date.parse()靜態(tài)方法。
Date.UTC()
Date.UTC()的參數(shù)分別是年,月(從0到11),日(1-31),時(shí)(0-23),分(0-59),秒(0-59),毫秒(0-999),最少參數(shù)2個(gè),即應(yīng)該包含年月,其他不填的默認(rèn)為0。
如果要?jiǎng)?chuàng)建的時(shí)間為中國(guó)標(biāo)準(zhǔn)時(shí)間的2015年5月20日,則代碼應(yīng)表示為
var myDate = new Date(Date.UTC(2015,5,19,16,0,0)) //Sat Jun 20 2015 00:00:00 GMT+0800 //格林威治時(shí)間 myDate.toUTCString() // Fri, 19 Jun 2015 16:00:00 GMT
其他:
var d = new Date(); //年 d.getFullYear() //月 d.getMonth() //日 d.getDate()
下面我們通過具體的示例來看看
/** * 日期時(shí)間腳本庫(kù)方法列表: * (1)Date.isValiDate:日期合法性驗(yàn)證 * (2)Date.isValiTime:時(shí)間合法性驗(yàn)證 * (3)Date.isValiDateTime:日期和時(shí)間合法性驗(yàn)證 * (4)Date.prototype.isLeapYear:判斷是否閏年 * (5)Date.prototype.format:日期格式化 * (6)Date.stringToDate:字符串轉(zhuǎn)成日期類型 * (7)Date.daysBetween:計(jì)算兩個(gè)日期的天數(shù)差 * (8)Date.prototype.dateAdd:日期計(jì)算,支持正負(fù)數(shù) * (9)Date.prototype.dateDiff:比較日期差:比較兩個(gè)時(shí)期相同的字段,返回相差值 * (10)Date.prototype.toArray:把日期分割成數(shù)組:按數(shù)組序號(hào)分別為:年月日時(shí)分秒 * (11)Date.prototype.datePart:取得日期數(shù)據(jù)信息 */ /** * 日期合法性驗(yàn)證:判斷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(默認(rèn)格式)或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){ return false; } if(!formatStr){ formatStr = "yyyy-MM-dd";//默認(rèn)格式:yyyy-MM-dd } if(dateStr.length!=formatStr.length){ return false; }else{ if(formatStr=="yyyy-MM-dd"||formatStr=="YYYY-MM-DD"){ var r1=/^(((((([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]))))))$/; return r1.test(dateStr); }else if(formatStr=="yyyy/MM/dd"||formatStr=="YYYY/MM/DD"){ var r2=/^(((((([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]))))))$/; return r2.test(dateStr); }else if(formatStr=="MM-dd-yyyy"||formatStr=="MM-DD-YYYY"){ var r3=/^((((((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])))$/; return r3.test(dateStr); }else if(formatStr=="MM/dd/yyyy"||formatStr=="MM/DD/YYYY"){ var r4=/^((((((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])))$/; return r4.test(dateStr); }else{ alert("日期格式不正確!"); return false; } } return false; } /** * 時(shí)間合法性驗(yàn)證:判斷timeStr是否符合formatStr指定的時(shí)間格式 * 示例: * (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(默認(rèn)格式) (2)hh-mm-ss (3)hh/mm/ss */ Date.isValiTime = function(timeStr, formatStr) { if(!timeStr){ return false; } if(!formatStr){ formatStr = "hh:mm:ss";//默認(rèn)格式:hh:mm:ss } if(timeStr.length!=formatStr.length){ return false; }else{ if(formatStr=="hh:mm:ss"){ var r1=/^(([0-1][0-9])|(2[0-3]))\:([0-5][0-9])\:([0-5][0-9])$/; return r1.test(timeStr); }else if(formatStr=="hh-mm-ss"){ var r2=/^(([0-1][0-9])|(2[0-3]))\-([0-5][0-9])\-([0-5][0-9])$/; return r2.test(timeStr); }else if(formatStr=="hh/mm/ss"){ var r3=/^(([0-1][0-9])|(2[0-3]))\/([0-5][0-9])\/([0-5][0-9])$/; return r3.test(timeStr); }else{ alert("時(shí)間格式不正確!"); return false; } } return false; } /** * 日期和時(shí)間合法性驗(yàn)證 * 格式:yyyy-MM-dd hh:mm:ss */ Date.isValiDateTime = function(dateTimeStr) { var dateTimeReg=/^(((((([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]))?$/ return dateTimeReg.test(dateTimeStr); } /** * 判斷閏年 :一般規(guī)律為:四年一閏,百年不閏,四百年再閏。 */ Date.prototype.isLeapYear = function() { return (this.getYear()%4==0&&((this.getYear()%100!=0)||(this.getYear()%400==0))); } /** * 日期格式化: * formatStr:可選,格式字符串,默認(rèn)格式:yyyy-MM-dd hh:mm:ss * 約定如下格式: * (1)YYYY/yyyy/YY/yy 表示年份 * (2)MM/M 月份 * (3)W/w 星期 * (4)dd/DD/d/D 日期 * (5)hh/HH/h/H 時(shí)間 * (6)mm/m 分鐘 * (7)ss/SS/s/S 秒 * (8)iii 毫秒 */ Date.prototype.format = function(formatStr) { var str = formatStr; if(!formatStr){ str = "yyyy-MM-dd hh:mm:ss";//默認(rèn)格式 } 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?(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())); return str; } /** * 字符串轉(zhuǎn)成日期類型: * dateStr:必選,日期字符串,如果無法解析成日期類型,返回null * 格式: * (1)yyyy/MM/dd:IE和FF通用 * (2)MM/dd/yyyy:IE和FF通用 * (3)MM-dd-yyyy:僅IE * (4)yyyy-MM-dd:非IE,且時(shí)鐘被解析在8點(diǎn)整 */ Date.stringToDate = function(dateStr) { if(!dateStr){ alert("字符串無法解析為日期"); return null; }else{ if(Date.isValiDate(dateStr,"yyyy/MM/dd")||Date.isValiDate(dateStr,"MM/dd/yyyy")){ return new Date(Date.parse(dateStr)); }else{ if((!-[1,])){//IE if(Date.isValiDate(dateStr,"MM-dd-yyyy")){ return new Date(Date.parse(dateStr)); }else{ alert("字符串無法解析為日期"); return null; } }else{//非IE if(Date.isValiDate(dateStr,"yyyy-MM-dd")){ return new Date(Date.parse(dateStr)); }else{ alert("字符串無法解析為日期"); return null; } } } } return null; } /** * 計(jì)算兩個(gè)日期的天數(shù)差: * dateOne:必選,必須是Data類型的實(shí)例 * dateTwo:必選,必須是Data類型的實(shí)例 */ Date.daysBetween = function(dateOne,dateTwo) { if((dateOne instanceof Date)==false||(dateTwo instanceof Date)==false){ return 0; }else{ return Math.abs(Math.floor((dateOne.getTime()-dateTwo.getTime())/1000/60/60/24)); } } /** * 日期計(jì)算:支持負(fù)數(shù),即可加可減,返回計(jì)算后的日期 * num:必選,必須是數(shù)字,且正數(shù)是時(shí)期加,負(fù)數(shù)是日期減 * field:可選,標(biāo)識(shí)是在哪個(gè)字段上進(jìn)行相加或相減,字段見如下的約定。無此參數(shù)時(shí),默認(rèn)為d * 約定如下格式: * (1)Y/y 年 * (2)M 月 * (3)W/w 周 * (4)D/d 日 * (5)H/h 時(shí) * (6)m 分 * (7)S/s 秒 * (8)Q/q 季 */ Date.prototype.dateAdd = function(num, field) { if((!num)||isNaN(num)||parseInt(num)==0){ return this; } if(!field){ field = "d"; } switch(field){ case 'Y': case 'y':return new Date((this.getFullYear()+num), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());break; case 'Q': case 'q':return new Date(this.getFullYear(), (this.getMonth()+num*3), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());break; case 'M':return new Date(this.getFullYear(), this.getMonth()+num, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());break; case 'W': case 'w':return new Date(Date.parse(this) + ((86400000 * 7) * num));break; case 'D': case 'd':return new Date(Date.parse(this) + (86400000 * num));break; case 'H': case 'h':return new Date(Date.parse(this) + (3600000 * num));break; case 'm':return new Date(Date.parse(this) + (60000 * num));break; case 'S': case 's':return new Date(Date.parse(this) + (1000 * num));break; default: return this; } return this; } /** * 比較日期差:比較兩個(gè)時(shí)期相同的字段,返回相差值 * dtEnd:必選,必須是Data類型的實(shí)例 * field:可選,標(biāo)識(shí)是在哪個(gè)字段上進(jìn)行比較,字段見如下的約定。無此參數(shù)時(shí),默認(rèn)為d * 約定如下格式: * (1)Y/y 年 * (2)M 月 * (3)W/w 周 * (4)D/d 日 * (5)H/h 時(shí) * (6)m 分 * (7)S/s 秒 */ Date.prototype.dateDiff = function(dtEnd, field) { var dtStart = this; if((dtEnd instanceof Date)==false){ return 0; }else{ if(!field){ field = "d"; } switch(field){ case 'Y': case 'y':return dtEnd.getFullYear() - dtStart.getFullYear();break; case 'M':return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);break; case 'W': case 'w':return parseInt((dtEnd - dtStart) / (86400000 * 7));break; case 'D': case 'd':return parseInt((dtEnd - dtStart) / 86400000);break; case 'H': case 'h':return parseInt((dtEnd - dtStart) / 3600000);break; case 'm':return parseInt((dtEnd - dtStart) / 60000);break; case 'S': case 's':return parseInt((dtEnd - dtStart) / 1000);break; default: return 0; } return 0; } } /** * 把日期分割成數(shù)組:按數(shù)組序號(hào)分別為:年月日時(shí)分秒 */ Date.prototype.toArray = function() { var myArray = new Array(); myArray[0] = this.getFullYear(); myArray[1] = this.getMonth(); myArray[2] = this.getDate(); myArray[3] = this.getHours(); myArray[4] = this.getMinutes(); myArray[5] = this.getSeconds(); return myArray; } /** * 取得日期數(shù)據(jù)信息: * field:可選,標(biāo)識(shí)是在哪個(gè)字段上進(jìn)行比較,字段見如下的約定。無此參數(shù)時(shí),默認(rèn)為d * (1)Y/y 年 * (2)M 月 * (3)W/w 周 * (4)D/d 日 * (5)H/h 時(shí) * (6)m 分 * (7)S/s 秒 */ Date.prototype.datePart = function(field) { if(!field){ field = "d"; } var Week = ['日','一','二','三','四','五','六']; switch (field){ case 'Y' : case 'y' :return this.getFullYear();break; case 'M' :return (this.getMonth()+1);break; case 'W' : case 'w' :return Week[this.getDay()];break; case 'D' : case 'd' :return this.getDate();break; case 'H' : case 'h' :return this.getHours();break; case 'm' :return this.getMinutes();break; case 's' :return this.getSeconds();break; default:return this.getDate(); } return this.getDate(); }
相關(guān)文章
淺談JSON.parse()和JSON.stringify()
本文給大家簡(jiǎn)單描述了下JSON.parse()和JSON.stringify()的異同點(diǎn),十分的實(shí)用,有需要的小伙伴可以參考下2015-07-07scrollWidth,clientWidth,offsetWidth的區(qū)別
這篇文章主要介紹了scrollWidth,clientWidth,offsetWidth的區(qū)別,需要的朋友可以參考下2015-01-01JavaScript實(shí)現(xiàn)煙花特效(面向?qū)ο?
這篇文章主要為大家詳細(xì)介紹了JavaScript使用面向?qū)ο缶幊虒?shí)現(xiàn)煙花特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08JavaScrip報(bào)錯(cuò):module?is?not?defined的原因及解決
這篇文章主要給大家介紹了關(guān)于JavaScrip報(bào)錯(cuò):module?is?not?defined的原因及解決方法,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09Bootstrap柵格系統(tǒng)簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Boostrap柵格系統(tǒng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03用JavaScript來美化HTML的select標(biāo)簽的下拉列表效果
這篇文章主要介紹了用JavaScript來美化HTML的select標(biāo)簽的下拉列表效果的方法,而且在多瀏覽器下的兼容效果也得到提升,需要的朋友可以參考下2015-11-11微信小程序?qū)崿F(xiàn)元素漸入漸出動(dòng)畫效果封裝方法
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)元素漸入漸出動(dòng)畫效果封裝方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05