feat(utils): 添加日期格式化工具函数
- 实现了将 yyyy-MM-dd HH:mm:ss 格式转换为 yyyy年MM月dd日HH时mm分格式的功能 - 添加了可自定义日期格式的功能,默认格式为 yyyy年MM月dd日HH时mm分 - 处理了空值情况,当传入日期为空时返回空字符串 - 完成了年、月、日、时、分的时间对象解析和替换逻辑master
parent
4c4ff42c0b
commit
c48bc8b654
|
|
@ -0,0 +1,14 @@
|
|||
//yyyy-MM-dd HH:mm:ss 转 yyyy年MM月dd日HH时mm分
|
||||
|
||||
export function formatDate(date, format = "yyyy年MM月dd日HH时mm分") {
|
||||
if (!date) {
|
||||
return "";
|
||||
}
|
||||
const dateObj = new Date(date);
|
||||
const year = dateObj.getFullYear();
|
||||
const month = dateObj.getMonth() + 1;
|
||||
const day = dateObj.getDate();
|
||||
const hour = dateObj.getHours();
|
||||
const minute = dateObj.getMinutes();
|
||||
return format.replace("yyyy", year).replace("MM", month).replace("dd", day).replace("HH", hour).replace("mm", minute);
|
||||
}
|
||||
Loading…
Reference in New Issue