diff --git a/packages/nc-gui-v2/utils/dateTimeUtils.ts b/packages/nc-gui-v2/utils/dateTimeUtils.ts index 2becbb5bca..62e3cf32e9 100644 --- a/packages/nc-gui-v2/utils/dateTimeUtils.ts +++ b/packages/nc-gui-v2/utils/dateTimeUtils.ts @@ -1,4 +1,6 @@ import dayjs from 'dayjs' +import customParseFormat from 'dayjs/plugin/customParseFormat' +dayjs.extend(customParseFormat) export const timeAgo = (date: any) => { return dayjs.utc(date).fromNow() @@ -18,3 +20,27 @@ export const handleTZ = (val: any) => { }, ) } + +export const dateFormat = [ + 'DD-MM-YYYY', + 'MM-DD-YYYY', + 'YYYY-MM-DD', + 'DD/MM/YYYY', + 'MM/DD/YYYY', + 'YYYY/MM/DD', + 'DD MM YYYY', + 'MM DD YYYY', + 'YYYY MM DD', +] + +export function validateDateFormat(v: string) { + return dateFormat.includes(v) +} + +export function validateDateWithUnknownFormat(v: string) { + let res = 0 + for (const format of dateFormat) { + res |= dayjs(v, format, true).isValid() as any + } + return res +}