一、日期转换
function timestampToDate(datetime, fmt = "yyyy-MM-dd HH:mm:ss") {
if (!datetime) {
console.error('参数错误:传入的参数为:', datetime);
return '[ 参数错误 ]';
}
const _dateTime = new Date(datetime);
const _weekDays = ["日", "一", "二", "三", "四", "五", "六"];
const _seasons = ["冬", "春", "夏", "秋"];
const o = {
"M+": _dateTime.getMonth() + 1,
"d+": _dateTime.getDate(),
"H+": _dateTime.getHours(),
"m+": _dateTime.getMinutes(),
"s+": _dateTime.getSeconds(),
"w+": _weekDays[_dateTime.getDay()],
"q+": _seasons[Math.floor((_dateTime.getMonth() + 3) / 3)],
S: _dateTime.getMilliseconds(),
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
(_dateTime.getFullYear() + "").substr(4 - RegExp.$1.length)
);
}
for (let k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1
? o[k]
: ("00" + o[k]).substr(("" + o[k]).length)
);
}
}
return fmt;
}