This commit is contained in:
oneao committed 2025-11-16 20:48:56 +08:00
1 parent c2c5cc92af
commit caed43a011
60 files changed
+1710 -1272

No files matched your search

@@ -27,3 +27,38 @@ export function getCurrentTime(format = 'HH:mm:ss'): string {
export function getCurrentYearMonth(format = 'YYYY-MM'): string {
return dayjs().format(format)
}
/** 字符串 / 数字 / Date → Date(无效返回 null) */
export function toDate(input: string | number | Date | undefined | null): Date {
if (!input)
return new Date()
if (input instanceof Date)
return input
const d = dayjs(input)
return d.isValid() ? d.toDate() : new Date()
}
/** Date → YYYY-MM-DD */
export function toDateString(date: string | number | Date): string {
const d = dayjs(toDate(date))
return d.isValid() ? d.format('YYYY-MM-DD') : ''
}
/** Date → YYYY-MM */
export function toYearMonthString(date: string | number | Date): string {
const d = dayjs(toDate(date))
return d.isValid() ? d.format('YYYY-MM') : ''
}
/** Date → HH:mm:ss */
export function toTimeString(date: string | number | Date): string {
const d = dayjs(toDate(date))
return d.isValid() ? d.format('HH:mm:ss') : ''
}
/** Date → YYYY-MM-DD HH:mm:ss */
export function toDateTimeString(date: string | number | Date): string {
const d = dayjs(toDate(date))
return d.isValid() ? d.format('YYYY-MM-DD HH:mm:ss') : ''
}