Date對(duì)象是JS提供的日期和時(shí)間操作接口。
靜態(tài)方法
Date.now()
Date.now
function now() { [native code] }
Date.now()
1505528339926 //返回當(dāng)前距離1970年1月1日00:00:00的毫秒數(shù),參考點(diǎn)哦哦。
Date.parse()
解析日期的字符串,返回距離1970年1月1日00:00:00的毫秒數(shù)。日期字符串的格式至少要符合
YYYY-MM-DDTHH:mm:ss.sssZ //Z是時(shí)區(qū)可選的,解析失敗返回NaN。
Date.parse('2011-10-10')
1318204800000
var a =Date.parse('2011-10-10')
undefined
a
1318204800000
Date()
返回當(dāng)前日期的字符串
Date()
"Sat Sep 16 2017 10:29:28 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)"
new Date()
生成一個(gè)日期對(duì)象,包含現(xiàn)在日期相關(guān)信息:
new Date()
Sat Sep 16 2017 10:31:51 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
//貌似生成了字符串,其實(shí)是生成對(duì)象太復(fù)雜了,在控制臺(tái)只能如此展示。
var a = new Date()
undefined
a
Sat Sep 16 2017 10:34:21 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
typeof a
"object" //對(duì)象哦
a.getDate()
16
a.getFullYear()
2017
a.getMonth()
8 //把月份排位了,0位是1月,這里的8是位置不是月份。
a.getHours()
10
a.getMinutes()
34
a.getSeconds()
21
a.getMilliseconds()
467
a.getDay()
6
new Date(2020,08,01)
Tue Sep 01 2020 00:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
//參數(shù)的話(huà),注意當(dāng)前的時(shí)區(qū)算起
str = '2017-08-01'
"2017-08-01"
new Date(str)
Tue Aug 01 2017 08:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
//字符串的話(huà),是八點(diǎn),因?yàn)槭菛|八區(qū),零時(shí)區(qū)是零點(diǎn)。
str='2017-08-01 00:00:00'
"2017-08-01 00:00:00"
new Date(str)
Tue Aug 01 2017 00:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
100天以前的時(shí)間是?
var curTime =Date.now()
undefined
new Date(curTime - 100*24*60*60*1000)
Thu Jun 08 2017 10:53:39 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
new Date(curTime - 100*24*60*60*1000).getMonth()
5
執(zhí)行時(shí)間段:
var start = Date.now()
undefined
var end=Date.now()
undefined
end - start
28051
有g(shù)et,就有set,用法類(lèi)似的。