类型转化
- yyyy-MM-dd hh:mm:ss 转化为 中国标准时间
new Date('yyyy-MM-dd hh:mm:ss') ---> Fri May 26 2023 17:59:19 GMT+0800 (中国标准时间)
- 中国标准时间转化 yyyy-MM-dd hh:mm:ss
export const formatDate = (date, fmt) => {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length))
}
const o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
S: date.getMilliseconds()
}
调用方法: formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
- yyyy-MM-dd hh:mm:ss 转为时间戳
let tempTime = '2023-05-26 18:00:21';
let timesResult = Date.parse(new Date(tempTime ));
- 中国标准时间转为时间戳
let tempTime = new Date();// Sat Jun 03 2023 10:14:50 GMT+0800 (中国标准时间)
const timesResult = tempTime.getTime()
- 时间戳转为中国标准时间
const tempTime = 1685095909000
const timesResult = new Data(time);