2022-10-26 20:49:29 +08:00
|
|
|
export function dateFormat2(fmt: string, date: Date) {
|
2021-06-07 17:22:07 +08:00
|
|
|
let ret;
|
|
|
|
|
const opt = {
|
2023-07-06 20:59:22 +08:00
|
|
|
'y+': date.getFullYear().toString(), // 年
|
|
|
|
|
'M+': (date.getMonth() + 1).toString(), // 月
|
|
|
|
|
'd+': date.getDate().toString(), // 日
|
|
|
|
|
'H+': date.getHours().toString(), // 时
|
|
|
|
|
'm+': date.getMinutes().toString(), // 分
|
|
|
|
|
's+': date.getSeconds().toString(), // 秒
|
2023-12-15 11:29:57 +08:00
|
|
|
'S+': date.getMilliseconds() ? date.getMilliseconds().toString() : '', // 毫秒
|
2021-06-07 17:22:07 +08:00
|
|
|
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
|
|
|
|
};
|
|
|
|
|
for (const k in opt) {
|
2023-07-06 20:59:22 +08:00
|
|
|
ret = new RegExp('(' + k + ')').exec(fmt);
|
2021-06-07 17:22:07 +08:00
|
|
|
if (ret) {
|
2023-07-06 20:59:22 +08:00
|
|
|
fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'));
|
2021-06-07 17:22:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fmt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function dateStrFormat(fmt: string, dateStr: string) {
|
2023-07-06 20:59:22 +08:00
|
|
|
return dateFormat2(fmt, new Date(dateStr));
|
2022-10-26 20:49:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function dateFormat(dateStr: string) {
|
2024-01-05 08:55:34 +08:00
|
|
|
if (!dateStr) {
|
2023-12-27 22:59:20 +08:00
|
|
|
return '';
|
|
|
|
|
}
|
2023-07-06 20:59:22 +08:00
|
|
|
return dateFormat2('yyyy-MM-dd HH:mm:ss', new Date(dateStr));
|
|
|
|
|
}
|