0%

JS中Date常用方法

new Date(参数可选)

返回都是Wed Jun 28 2023 09:16:31 GMT+0800 (中国标准时间)格式的日期。
如果没有参数就是当前系统时间。
参数格式可以是以下形式

1
2
3
4
'December 17, 1995 03:24:00'
'1995-12-17T03:24:00'
1995, 11, 17
1995, 11, 17, 3, 24, 0
得到当前时间戳,(1-31)日、星期日到六(0-6)
1
2
3
4
5
6
7
8
let now = Date.now()
//也可以
let now = new Date().getTime()

//日
getDate()
//星期
getDay
Date.parse()

解析参数的日期,返回时间戳,但是后三位置0

1
2
3
4
5
'Wed Jun 28 2023 09:16:31 GMT+0800 (中国标准时间)'
'December 17, 1995 03:24:00'
'1995-12-17T03:24:00'
1995, 11, 17
1995, 11, 17, 3, 24, 0
最近的xx天
1
2
3
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * xx);
得到天或者星期几

get