创建日期
new Date()可以传入一个参数
- 日期格式字符串
- 时间戳
- new Date(year, month, day, hours, minutes, seconds, milliseconds)
- 不传
关于日期获取的一些方法
- 时间戳
new Date().getTime() - 年
getFullYear() - 月
getMonth() - 日
getDate() - 时
getHour() - 分
getMinutes() - 秒
getSeconds() - 毫秒
getMilliseconds()
格式化日期
通过上面的方法可以将日期组合成你所需要的方式,如YYYY-MM-DD HH:mm:ss
const date = new Date();
const year = date.getFullYear();
let month = date.getFullMonth();
month = month>9?month:'0'+month
let day = date.getDate();
day = day>9?day:'0'+day
let hour = date.getHour();
hour = hour>9?hour:'0'+hour
let minutes = date.getMinutes();
minutes = minutes>9?minutes:'0'+minutes
let s = date.getSeconds();
s = s>9?s:'0'+s
const nowDa = year+'-'+month+'-'+day+' ' + hour+':'+minutes+':'+date;
日期的比较
可以直接使用>、<等等比较符进行比较
比较两个日期相等不能使用==或者===,可以使用 getTime()进行比较