From 6680e7264576d455c47d5fc47cda8c70561c4d32 Mon Sep 17 00:00:00 2001 From: pany <939630029@qq.com> Date: Mon, 11 Nov 2024 19:06:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E5=86=99=20formatDateTime?= =?UTF-8?q?=20=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/datetime.ts | 9 +++++++++ src/utils/index.ts | 6 ------ 2 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 src/utils/datetime.ts diff --git a/src/utils/datetime.ts b/src/utils/datetime.ts new file mode 100644 index 0000000..c65597d --- /dev/null +++ b/src/utils/datetime.ts @@ -0,0 +1,9 @@ +import dayjs from "dayjs" + +const INVALID_DATE = "N/A" + +/** 格式化日期时间 */ +export const formatDateTime = (datetime: string | number | Date = "", template: string = "YYYY-MM-DD HH:mm:ss") => { + const day = dayjs(datetime) + return day.isValid() ? day.format(template) : INVALID_DATE +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 290c7e4..dfd7a5e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,11 +1,5 @@ -import dayjs from "dayjs" import { removeConfigLayout } from "@/utils/cache/local-storage" -/** 格式化时间 */ -export const formatDateTime = (time: string | number | Date) => { - return time ? dayjs(new Date(time)).format("YYYY-MM-DD HH:mm:ss") : "N/A" -} - /** 用 JS 获取全局 css 变量 */ export const getCssVariableValue = (cssVariableName: string) => { let cssVariableValue = ""