From c48bc8b654796711239e94d685311a61bd6d2196 Mon Sep 17 00:00:00 2001 From: fangjiakai <450850793@qq.com> Date: Fri, 16 Jan 2026 08:51:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(utils):=20=E6=B7=BB=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=B7=A5=E5=85=B7=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现了将 yyyy-MM-dd HH:mm:ss 格式转换为 yyyy年MM月dd日HH时mm分格式的功能 - 添加了可自定义日期格式的功能,默认格式为 yyyy年MM月dd日HH时mm分 - 处理了空值情况,当传入日期为空时返回空字符串 - 完成了年、月、日、时、分的时间对象解析和替换逻辑 --- src/utils/dateFormat.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/utils/dateFormat.js diff --git a/src/utils/dateFormat.js b/src/utils/dateFormat.js new file mode 100644 index 0000000..52febbc --- /dev/null +++ b/src/utils/dateFormat.js @@ -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); +} \ No newline at end of file