diff --git a/packages/nc-gui/utils/dateTimeUtils.ts b/packages/nc-gui/utils/dateTimeUtils.ts index a83ec9c364..6339fa0776 100644 --- a/packages/nc-gui/utils/dateTimeUtils.ts +++ b/packages/nc-gui/utils/dateTimeUtils.ts @@ -1,4 +1,7 @@ import dayjs from 'dayjs' +import customParseFormat from 'dayjs/plugin/customParseFormat' + +dayjs.extend(customParseFormat) export const timeAgo = (date: any) => { return dayjs.utc(date).fromNow() @@ -36,11 +39,17 @@ export function validateDateFormat(v: string) { } export function validateDateWithUnknownFormat(v: string) { - let res = 0 for (const format of dateFormats) { - res |= dayjs(v, format, true).isValid() as any + if (dayjs(v, format, true).isValid() as any) { + return true + } + for (const timeFormat of ['HH:mm', 'HH:mm:ss', 'HH:mm:ss.SSS']) { + if (dayjs(v, `${format} ${timeFormat}`, true).isValid() as any) { + return true + } + } } - return res + return false } export function getDateFormat(v: string) { @@ -52,6 +61,18 @@ export function getDateFormat(v: string) { return 'YYYY/MM/DD' } +export function getDateTimeFormat(v: string) { + for (const format of dateFormats) { + for (const timeFormat of ['HH:mm', 'HH:mm:ss', 'HH:mm:ss.SSS']) { + const dateTimeFormat = `${format} ${timeFormat}` + if (dayjs(v, dateTimeFormat, true).isValid() as any) { + return dateTimeFormat + } + } + } + return 'YYYY/MM/DD' +} + export function parseStringDate(v: string, dateFormat: string) { const dayjsObj = dayjs(v) if (dayjsObj.isValid()) {