From b5b7ce0ef9a6ff45fc5a06fb763360cfeca7281c Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Thu, 8 Dec 2022 18:31:49 +0800 Subject: [PATCH] feat(nocodb): add dateTimeUtils --- .../nocodb/src/lib/utils/dateTimeUtils.ts | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 packages/nocodb/src/lib/utils/dateTimeUtils.ts diff --git a/packages/nocodb/src/lib/utils/dateTimeUtils.ts b/packages/nocodb/src/lib/utils/dateTimeUtils.ts new file mode 100644 index 0000000000..19c058df34 --- /dev/null +++ b/packages/nocodb/src/lib/utils/dateTimeUtils.ts @@ -0,0 +1,75 @@ +import dayjs from 'dayjs'; + +export const dateFormats = [ + '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 dateFormats.includes(v); +} + +export function validateDateWithUnknownFormat(v: string) { + for (const format of dateFormats) { + 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 false; +} + +export function getDateFormat(v: string) { + for (const format of dateFormats) { + if (dayjs(v, format, true).isValid()) { + return format; + } + } + 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()) { + v = dayjsObj.format('YYYY-MM-DD'); + } else { + v = dayjs(v, dateFormat).format('YYYY-MM-DD'); + } + return v; +} + +export function convertToTargetFormat( + v: string, + oldDataFormat, + newDateFormat: string +) { + if ( + !dateFormats.includes(oldDataFormat) || + !dateFormats.includes(newDateFormat) + ) + return v; + return dayjs(v, oldDataFormat).format(newDateFormat); +}