-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.ts
More file actions
81 lines (63 loc) · 1.7 KB
/
utils.ts
File metadata and controls
81 lines (63 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
import { format, isToday, isTomorrow } from 'date-fns'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export const getDateObect = (val) => {
let date
if (!val) {
return null
}
if (Object.prototype.toString.call(val) === '[object Date]') {
if (isNaN(val)) {
return null
}
}
if (typeof val.toDate === 'function') {
date = val.toDate()
} else {
date = new Date(val)
}
return date
}
export const formatDate = (val, formatStr, locale) => {
if (!val) {
return ''
}
const date = getDateObect(val)
if (!date) {
return ''
}
const options = {}
// if (dateLocales[locale]) {
// options = { locale: dateLocales[locale] }
// }
return format(date, formatStr, options)
}
export const getYmd = (val, locale) => {
return formatDate(val, 'yyyy-MM-dd', locale)
}
export const getDay = (val, locale) => {
return formatDate(val, 'iiii', locale)
}
export const getDateTime = (val: any, locale = 'en') => {
return formatDate(val, 'dd MMM yyyy, HH:mm', locale)
}
export const getDate = (val: any, locale?: string): string => {
const date = getDateObect(val)
if (!date) return ''
if (isToday(date)) return 'Today'
if (isTomorrow(date)) return 'Tomorrow'
return formatDate(date, 'd MMM', locale)
}
export const getDateFull = (val: any, locale?: string): string => {
const date = getDateObect(val)
if (!date) return ''
if (isToday(date)) return 'Today'
if (isTomorrow(date)) return 'Tomorrow'
return formatDate(date, 'd MMM yyyy', locale)
}
export const getTime = (val, locale) => {
return formatDate(val, 'HH:mm', locale)
}