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 = ""