目录
获取时间
获取当前时间的时间戳的两种方式
1、 Date.now()
获取从 1970 年 1 月 1 日 0 时 0 分 0 秒(UTC,即协调世界时)到当前时间的毫秒数
2、new Date().getTime()
获取 从 1970 年 1 月 1 日 0 时 0 分 0 秒(UTC,即协调世界时)距离 Date 对象所代表时间的毫秒数。(更早的时间会用负数表示)
//获取当前时间-时间戳
const currentTime1 = Date.now()
console.log('currentTime1: ', currentTime1)
const currentTime12 = new Date().getTime()
console.log('currentTime12: ', currentTime12)
获取年月日时分秒
Date()构造方法的参数说明
首先需要知道Date构造方法参数有以下几种情况
1、Date()
2、Date(时间字符串)
3、Date(时间戳)
4、Date(年,月,日,时,分,秒)
获取时间的方法
1、获取年getFullYear()
2、获取月
3、获取一个月中的第几天
4、获取一周中的第几天
5、获取时
6、获取分
7、获取秒
示例
// Date()不传递参数
const date0 = new Date()
const year0 = date0.getFullYear()
const month0 = date0.getMonth() + 1
const day0 = date0.getDate()
const week0 = date0.getDay()
const hour0 = date0.getHours();
const minute0 = date0.getMinutes();
const second0 = date0.getSeconds();
console.log('year: ', year0, ' month: ', month0, ' day: ', day0, ' week: ', week0, ' hour: ', hour0,
' minute: ',
minute0, ' second: ', second0)
// Date传递时间字符串