Date
Date對象是JavaScript提供的日期和時間的操作接口
Date對象有幾個靜態(tài)方法
- Date.now()
now方法返回當(dāng)前距離1970年1月1日00:00:00的毫秒數(shù)
Date.now(); // 1427974222853
- Date.parse()
parse方法用來解析日期字符串智绸,返回距離1970年1月1日 00:00:00的毫秒數(shù)
日期字符串的格式應(yīng)該完全或者部分符合YYYY-MM-DDTHH:mm:ss.sssZ格式靴患,Z表示時區(qū),是可選的
如果解析失敗,返回NaN
Date.parse("January 26, 2011 13:51:50")
Date.parse("Mon, 25 Dec 1995 13:30:00 GMT")
Date.parse("Mon, 25 Dec 1995 13:30:00 +0430")
Date.parse("2011-10-10")
Date.parse("2011-10-10T14:48:00")
- Date.UTC()
默認情況下,Date對象返回的都是當(dāng)前時區(qū)的時間
Date.UTC方法可以返回UTC時間。該方法接受年、月、日等變量作為參數(shù)竖幔,返回當(dāng)前距離1970年1月1日 00:00:00 UTC的毫秒數(shù)
Date.UTC(2015,0,1,2,3,4,567); //1420077784567
- Date()
Date函數(shù)可以直接調(diào)用,返回一個當(dāng)前日期和時間的字符串是偷,這時候是否有參數(shù)結(jié)果一樣
Date(); // "Thu Apr 02 2015 19:20:29 GMT+0800 (CST)"
- new Date()
使用Date構(gòu)造函數(shù)創(chuàng)建一個Date的實例
new Date();
實例時間為當(dāng)前時間拳氢,同時實例有些方法get
Date.prototype.getTime():返回實例對象距離1970年1月1日00:00:00對應(yīng)的毫秒數(shù),等同于valueOf方法
Date.prototype.getDate():返回實例對象對應(yīng)每個月的幾號(從1開始)
Date.prototype.getDay():返回星期蛋铆,星期日為0馋评,星期一為1,以此類推
Date.prototype.getFullYear():返回四位的年份
Date.prototype.getMonth():返回月份(0表示1月刺啦,11表示12月)
Date.prototype.getHours():返回小時(0-23)
Date.prototype.getMilliseconds():返回毫秒(0-999)
Date.prototype.getMinutes():返回分鐘(0-59)
Date.prototype.getSeconds():返回秒(0-59)
Date.prototype.getTimezoneOffset():返回當(dāng)前時間與UTC的時區(qū)差異留特,以分鐘表示,返回結(jié)果考慮到了夏令時因素
- set
Date.prototype.setDate(date):設(shè)置實例對象對應(yīng)的每個月的幾號(1-31)玛瘸,返回改變后毫秒時間戳
Date.prototype.setFullYear(year [, month, date]):設(shè)置四位年份
Date.prototype.setHours(hour [, min, sec, ms]):設(shè)置小時(0-23)
Date.prototype.setMilliseconds():設(shè)置毫秒(0-999)
Date.prototype.setMinutes(min [, sec, ms]):設(shè)置分鐘(0-59)
Date.prototype.setMonth(month [, date]):設(shè)置月份(0-11)
Date.prototype.setSeconds(sec [, ms]):設(shè)置秒(0-59)
Date.prototype.setTime(milliseconds):設(shè)置毫秒時間戳
- Date.prototype.toString()
toString方法返回一個完整的時間字符串
var today = new Date();
today.toString(); // "Fri Apr 03 2015 11:17:29 GMT+0800 (CST)"
Date.prototype.toUTCString()蜕青,Date.prototype.toISOString()
toUTCString方法返回對應(yīng)的UTC時間,也就是比北京時間晚8個小時糊渊。toISOString方法返回對應(yīng)時間的ISO8601寫法右核。
var today = new Date(1362790014000);
today.toUTCString(); //
today.toISOString(); //
- Date.prototype.toDateString(),Date.prototype.toTimeString()
toDateString方法返回日期的字符串形式渺绒,toTimeString方法返回時間的字符串形式贺喝。
var today = new Date(1362790014000);
today.toDateString(); // "Fri Apr 03 2015"
today.toTimeString(); // "11:17:29 GMT+0800 (CST)"
toLocalDateString, toLocalTimeString
toLocalDateString方法返回一個字符串,代表日期的當(dāng)?shù)貙懛?/p>
toLocalTimeString方法返回一個字符串宗兼,代表時間的當(dāng)?shù)貙懛?/p>
var today = new Date(1362790014000);
today.toLocaleDateString(); // "2015/4/3"
today.toLocaleTimeString(); // "上午11:17:29"
- new Date(milliseconds)
Date對象接受從1970年1月1日00:00:00 UTC開始計算的毫秒數(shù)作為參數(shù)躏鱼。這意味著如果將Unix時間戳作為參數(shù),必須將Unix時間戳乘以1000殷绍。
new Date(1378218728000); // Tue Sep 03 2013 22:32:08 GMT+0800 (CST)
// 1970年1月2日的零時
var Jan02_1970 = new Date(3600241000); // Fri Jan 02 1970 08:00:00 GMT+0800 (CST)
- new Date(datestring)
Date對象還接受一個日期字符串作為參數(shù)染苛,返回所對應(yīng)的時間。所有可以被Date.parse()方法解析的日期字符串主到,都可以當(dāng)作Date對象的參數(shù)
new Date("2013-02-15")
new Date("2013-FEB-15")
new Date("FEB, 15, 2013")
new Date("FEB 15, 2013")
new Date("Feberuary, 15, 2013")
new Date("Feberuary 15, 2013")
new Date("15, Feberuary, 2013")
new Date(year, month [, day, hours, minutes, seconds, ms])
在多個參數(shù)的情況下茶行,Date對象將其分別視作對應(yīng)的年贸呢、月、日拢军、小時、分鐘怔鳖、秒和毫秒茉唉。如果采用這種用法,最少需要指定兩個參數(shù)(年和月)结执,其他參數(shù)都是可選的度陆,默認等于0。如果只使用年一個參數(shù)献幔,Date對象會將其解釋為毫秒數(shù)懂傀。
new Date(2013) // Thu Jan 01 1970 08:00:02 GMT+0800 (CST)
new Date(2013,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST)
new Date(2013,0,1) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST)
new Date(2013,0,1,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST)
new Date(2013,0,1,0,0,0,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST)
上面代碼(除了第一行)返回的是2013年1月1日零點的時間,可以看到月份從0開始計算蜡感,因此1月是0蹬蚁,12月是11。但是郑兴,月份里面的天數(shù)從1開始計算犀斋。
這些參數(shù)如果超出了正常范圍,會被自動折算情连。比如叽粹,如果月設(shè)為15,就算折算為下一年的4月却舀。參數(shù)還可以使用負數(shù)虫几,表示扣去的時間。
new Date(2013,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST)
new Date(2013,-1) // Sat Dec 01 2012 00:00:00 GMT+0800 (CST)
new Date(2013,0,1) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST)
new Date(2013,0,0) // Mon Dec 31 2012 00:00:00 GMT+0800 (CST)
new Date(2013,0,-1) // Sun Dec 30 2012 00:00:00 GMT+0800 (CST)
- 日期運算
類型轉(zhuǎn)換時挽拔,Date對象的實例如果轉(zhuǎn)為數(shù)值辆脸,則等于對應(yīng)的毫秒數(shù);如果轉(zhuǎn)為字符串篱昔,則等于對應(yīng)的日期字符串每强。所以,兩個日期對象進行減法運算州刽,返回的就是它們間隔的毫秒數(shù)空执;進行加法運算,返回的就是連接后的兩個字符串穗椅。
var then = new Date(2013,2,1);
var now = new Date(2013,3,1);
now - then
// 2678400000
now + then
// "Mon Apr 01 2013 00:00:00 GMT+0800 (CST)Fri Mar 01 2013 00:00:00 GMT+0800 (CST)"