Browse Source

feat(nc-gui): add customParseFormat & revise datetime util functions

pull/4135/head
Wing-Kam Wong 2 years ago
parent
commit
538524f4bd
  1. 27
      packages/nc-gui/utils/dateTimeUtils.ts

27
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()) {

Loading…
Cancel
Save