一、时间格式转换
1、通用型
时间格式化,如果要多种格式化,可以使用工具 moment.js
function formatTime(number, format = 'Y-M-D') {
let formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
let returnArr = [];
// let date = getDate(number); // 微信wxs
let date = new Date(number); // 通用js
returnArr.push(date.getFullYear());
returnArr.push(formatNumber(date.getMonth() + 1));
returnArr.push(formatNumber(date.getDate()));
returnArr.push(formatNumber(date.getHours()));
returnArr.push(formatNumber(date.getMinutes()));
returnArr.push(formatNumber(date.getSeconds()));
returnArr.forEach(function(item,index) {
format = format.replace(formateArr[index], item);
})
function formatNumber (n){
n = n.toString()
return n[1] ? n : '0' + n
}
return format;
}
使用案例:
formatTime('2022-5-1', 'Y/D/M h:m:s')
// 结果: 2022/05/1 00:00:00