一、时间戳转成日期格式:
日期补零操作
let checkTime = function(i){
if(m<10){
m= '0' + m
}
return m;
}
1、let date = new Date() // 得到一个时间对象
date.getFullYear() 获取完整的年份
date.getMonth() 获取月份(0-11,0代表1月,用的时候要加1)
date.getDate() 获取日(1-31)
date.getTime() 获取时间(从1970.1.1开始的毫秒数)
date.getHours() 获取小时数(0-23)
date.getMinutes() 获取分钟数(0-59)
date.getSeconds() 获取秒数(0-59)
2、获取年月日
let currentYMD = date.getFullYear() + '-' + checkTime(date.getMonth() + 1) + '-' + checkTime(date.getDate())
console.log(currentYMD) // 2020-01-17 年月日
3、获取年月日时分秒
let currentHMS = date.getFullYear() + '-' + checkTime(date.getMonth() + 1) + '-' + checkTime(date.getDate()) + '-' + checkTime(date.getHours()) + '-' + checkTime(date.getMinutes()) + '-' + checkTime(date.getSeconds()) console.log(currentHMS) // 2020-01-17-13-48-26 时分秒
4、 获取时间(从1970.1.1开始的毫秒数)
二、日期格式转时间戳
let time = '2019-01-17 14:27:13:12'
let date = new Date(time) // 不传就是获取当前时间,但是这样做不兼容火狐
time1 = date.getTime() // 精确到毫秒
time2 = date.valueOf() // 精确到毫秒
time3 = Date.parse(date) // 精确到秒,毫秒用0替代
console.log(time1) // 1547706433012
console.log(time2) // 1547706433012
console.log(time3) // 1547706433000
三、Date()参数形式有4种
1、new Date('2020,02,02,01:01:01:01')
比打印出来的日期多一天 // 2020-02-01T17:01:01.001Z
2、new Date('2020/02/02,01:01:01') // 2020-02-01T17:01:01.001Z
3、new Date('September 20 2019 09:09:09') //2019-09-20T01:09:09.000Z 相差8个小时new Date日期减去8
4、new Date('2019,09,09') // 2019-09-08T16:00:00.000Z