JS Date

Date对象是JS提供的日期和时间操作接口。

静态方法

Date.now()

Date.now
function now() { [native code] }
Date.now()
1505528339926                    //返回当前距离1970年1月1日00:00:00的毫秒数,参考点哦哦。

Date.parse()

解析日期的字符串,返回距离1970年1月1日00:00:00的毫秒数。日期字符串的格式至少要符合

YYYY-MM-DDTHH:mm:ss.sssZ    //Z是时区可选的,解析失败返回NaN。
Date.parse('2011-10-10')
1318204800000
var a =Date.parse('2011-10-10')
undefined
a
1318204800000

Date()

返回当前日期的字符串

Date()
"Sat Sep 16 2017 10:29:28 GMT+0800 (中国标准时间)"

new Date()

生成一个日期对象,包含现在日期相关信息:

new Date()
Sat Sep 16 2017 10:31:51 GMT+0800 (中国标准时间)
//貌似生成了字符串,其实是生成对象太复杂了,在控制台只能如此展示。
var a = new Date()
undefined
a
Sat Sep 16 2017 10:34:21 GMT+0800 (中国标准时间)
typeof a
"object"   //对象哦
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 (中国标准时间)
//参数的话,注意当前的时区算起
str = '2017-08-01'
"2017-08-01"
new Date(str)
Tue Aug 01 2017 08:00:00 GMT+0800 (中国标准时间)
//字符串的话,是八点,因为是东八区,零时区是零点。
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 (中国标准时间)

100天以前的时间是?
var curTime =Date.now()
undefined
new Date(curTime - 100*24*60*60*1000)
Thu Jun 08 2017 10:53:39 GMT+0800 (中国标准时间)
new Date(curTime - 100*24*60*60*1000).getMonth()
5
执行时间段:
var start = Date.now()
undefined
var end=Date.now()
undefined
end - start
28051

有get,就有set,用法类似的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容