forked from Nick-Mazuk/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.ts
73 lines (65 loc) · 1.63 KB
/
time.ts
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
const monthsFull = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]
const monthsShort = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
]
export const isValidDate = (date: string | number): boolean => {
if (typeof date === 'number') {
if (date < 0) return false
if (date % 1 !== 0) return false
return true
}
const parsedDate = Date.parse(date)
return !isNaN(parsedDate) && date !== ''
}
export const dateToMonthDDYYYY = (input: string | number): string => {
if (!isValidDate(input)) return ''
const date = new Date(input)
const month = monthsFull[date.getMonth()]
const day = date.getDate()
const year = date.getFullYear()
return `${month} ${day}, ${year}`
}
export const dateToMonDDYYYY = (input: string | number): string => {
if (!isValidDate(input)) return ''
const date = new Date(input)
const month = monthsShort[date.getMonth()]
const day = date.getDate()
const year = date.getFullYear()
return `${month} ${day}, ${year}`
}
export const dateToMDYYYY = (input: string | number): string => {
if (!isValidDate(input)) return ''
const date = new Date(input)
const month = date.getMonth() + 1
const day = date.getDate()
const year = date.getFullYear()
return `${month}/${day}/${year}`
}
export const dateStringToMilli = (dateString: string): number => {
if (!isValidDate(dateString)) return -1
return Date.parse(dateString)
}