Date類型使用自UTC時間1970年1月1日零點開始經(jīng)過的毫秒數(shù)來保存日期
1.創(chuàng)建一個日期對象
var now = new Date(); //不傳參的情況下罐寨,新創(chuàng)建的對象自動獲取當前的日期和時間
Date(); //Date是個構(gòu)造函數(shù),可以直接調(diào)用鸯绿,返回一個當前日期和時間的字符串
2.靜態(tài)方法
-
Date.now()
:返回調(diào)用該方法的當前時間距離1970年1月1日00:00:00的毫秒數(shù)
Date.now(); //1487752633237
-
Date.parse()
:接收一個表示日期的字符串參數(shù),返回相應(yīng)日距離1970年1月1日 00:00:00的毫秒數(shù)
Date.parse("2011-10-10"); //1318204800000瓶蝴;解析失敗,返回NaN
//日期字符串的格式:
//"月/日/年"租幕,如6/12/2015
//"英文月 日,年",如March 24,2012
//"英文星期幾 英文月 日 年 時:分:秒 時區(qū)"劲绪,如Tue May 23 2014 00:00:00 GMT-0700
//YYYY-MM-DDTHH:mm:ss.sssZ格式,Z表示時區(qū)贾富,可選歉眷;兼容ES5的支持這種格式
new Date( Date.parse("2011-10-10") ); //Mon Oct 10 2011 08:00:00 GMT+0800 (中國標準時間)
new Date("2011-10-10"); //Mon Oct 10 2011 08:00:00 GMT+0800 (中國標準時間)
//上面2種寫法獲取到了同樣的時間颤枪,說明可以直接將表示日期的字符串傳給Date構(gòu)造函數(shù),會在后臺調(diào)用Date.parse()汇鞭;因此庸追,以上2種寫法等價
-
Date.UTC()
:同樣返回表示日期的毫秒數(shù),接收的參數(shù)分別為年台囱、月(基于0的月份,0表示1月簿训,以此類推),以及可選參數(shù)日(默認為1)强品、時膘侮、分的榛、秒(參數(shù)時、分夫晌、秒默認為0);
Date.UTC(2000,9,9); //971049600000
new Date( Date.UTC(2000,9,9) ); //Mon Oct 09 2000 08:00:00 GMT+0800 (中國標準時間)
new Date(2000,9,9) //Mon Oct 09 2000 08:00:00 GMT+0800 (中國標準時間)
//由此可見晓淀,Date構(gòu)造函數(shù)也會模仿Date.UTC(),接收的參數(shù)也與之相同
//有一點不同的是凶掰,日期和時間都基于本地時區(qū)
3.寫一個函數(shù)getIntv
,獲取從當前時間到指定日期的間隔時間
function getIntv(tim){
var ntim = Date.now(),
tim = Date.parse(tim),
differTim;
if(tim >= ntim){
differTim = tim - ntim;
}else{
differTim = ntim - tim;
}
var days = Math.floor( differTim / (1000*60*60*24) ),
hours = Math.floor( differTim / (1000*60*60) - 24*days),
minutes = Math.floor( differTim / (1000*60) - days*24*60 - hours*60 ),
seconds = Math.floor( differTim / 1000 - days*24*60*60 - hours*60*60 - minutes*60),
str = "距指定日期" + days + "天" + hours + "小時" + minutes + "分" + seconds + "秒";
return str;
}
console.log( getIntv("2017-04-29") ); //距指定日期30天17小時17分15秒
4.把數(shù)字日期改成中文日期
function getChsDate(dateStr){
var numData = {
"0": "零","1": "一","2": "二","3": "三","4": "四","5": "五",
"6": "六","7": "七","8": "八","9": "九","10": "十","11": "十一",
"12": "十二","13": "十三","14": "十四","15": "十五","16": "十六",
"17": "十七","18": "十八","19": "十九","20": "二十","21": "二十一",
"22": "二十二","23": "二十三","24": "二十四","25": "二十五",
"26": "二十六","27": "二十七","28": "二十八","29": "二十九",
"30": "三十","31": "三十一",
}
var dateStr = dateStr.split("-"),
yy = dateStr[0],
mm = dateStr[1],
dd = dateStr[2];
var yer = numData[ yy[0] ] + numData[ yy[1] ] + numData[ yy[2] ] + numData[ yy[3] ] + "年",
mth = mm[0] == 0 ? numData[ mm[1] ] + "月" : numData[mm] + "月",
dte = dd[0] == 0 ? numData[ dd[1] ] + "日" : numData[dd] + "日";
return yer + mth +dte;
}
console.log( getChsDate('2015-12-19') ); //二零一五年十二月十九日
console.log( getChsDate('2015-02-09') ); //二零一五年二月九日
5.寫一個函數(shù)獲取n天前的日期
function getLastNDays(n){
var ntim = Date.now(),
nsec = n *1000 *60 *60 *24,
during = ntim - nsec,
date = new Date(during);
var yer = date.getFullYear(),
mth = ( date.getMonth() + "" ).length == 1 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1),
dat = ( date.getDate() + "" ).length == 1 ? "0" + date.getDate() : date.getDate();
return yer + "-" + mth + "-" + dat;
}
console.log( getLastNDays(3) ); //2017-3-29
console.log( getLastNDays(30) ); //2017-3-2
6.獲取執(zhí)行時間
var Runtime = (function(){
return {
start: function(){
sta = Date.now();
},
end: function(){
ent = Date.now();
},
get: function(){
var dtim = ent - sta;
return dtim / 1000 + "秒";
}
};
}());
Runtime.start();
for(var i=0;i<10000;i++){
console.log(1);
}
Runtime.end();
console.log( Runtime.get() ); //1.338秒