如何使用ES6中的模板字符串以及new Date()中自带的方法将中国标准时间转换为yyyy-MM-dd
const date = new Date(); const formattedDate = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`; console.log(formattedDate); // 输出类似 '2023-08-22 15:03:00' 的字符串
如何将 yyyy-MM-dd HHmmss转化为中国标准时间GMT
overTimeDeal(overTime) {
const year = overTime.slice(0, 4) || '0000';
const month = overTime.slice(4, 6) || '00';
const day = overTime.slice(6, 8) || '00';
const HHmmss= overTime.slice(-6) || '100000';
const timeStamp = Date.parse(`${year}-${month}-${day} ${HHmmss}`);
return new Date(timeStamp);
},
本文介绍了如何在JavaScript中使用ES6的模板字符串配合newDate方法,实现中国标准时间(yyyy-MM-dd)的格式化,以及如何将非标准格式如yyyy-MM-ddHHmmss转换为标准时间。
891





