一、YYYY-MM-DD转时间戳

let date = ' 1999-09-21 ';

let res = Date.parse(new Date(date)) / 1000

console.log(res) // 937872000

二、标准时间转YYYY-MM-DD

let res = new Date().toLocaleDateString().slice().replace(/\//g,'-')

console.log(res) // 2022-4-18

三、标准时间转YYYY-MM-DD HH:MM:SS

let date = new Date()

console.log(date) // 标准时间 Mon Apr 18 2022 15:16:28 GMT+0800 (中国标准时间)

let year = date.getFullYear();

console.log(year) // 取得4位数的年份 2022

let month = date.getMonth() + 1

console.log(month) // 取得日期中的月份 4;其中0表示1月,11表示12月;

let day = date.getDate();

console.log(day) // 取得日期月份中的天数 18(1到31)

let hour = date.getHours()

console.log(hour) // 取得日期中的小时数 (0到23)

let minute = date.getMinutes()

console.log(minute) // 取得日期中的分钟数 (0到59)

let second = date.getSeconds()

console.log(second) // 取得日期中的秒数 (0 到59)

let res = year + '-' + month + '-' + day + '-' + hour + ':' + minute + ':' + sencond;

console.log(res) // 2022-4-18 15:39:50

四、时间戳转YYYY-MM-DD HH:MM:SS

let date = new Date(1650211200) // 标准时间戳

先把时间戳转换为标准时间,然后在执行

1、时间戳转时间

let timestamp = 1650211200 // 获取时间戳

let res = new Date(parseInt(timestamp) * 1000).toLocaleDateString().replace(/年|月/g,"-").replace(/日/g," ")

console.log(res) // 2022/4/18

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐