From d94adf038ce1375de5dde1f58c584d80a6b6605d Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Thu, 4 Aug 2022 19:36:03 +0800 Subject: [PATCH] feat(gui-v2): add date function for formulas --- packages/nc-gui-v2/utils/dateTimeUtils.ts | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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 +}