-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathdate.js
More file actions
109 lines (104 loc) · 2.7 KB
/
date.js
File metadata and controls
109 lines (104 loc) · 2.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function toDate(maybeDate) {
if (!(maybeDate instanceof Date)) return new Date(maybeDate)
return maybeDate
}
function getWeekOfYear(date) {
const d = toDate(date)
const firstOfYear = new Date(d.getUTCFullYear(), 0, 1, 0 - d.getTimezoneOffset() / 60)
const week = 7 * 24 * 60 * 60 * 1000
return ((d - firstOfYear) / week).toFixed(0)
}
// important: keys must be sorted from longest to shortest for matching
const dateTokens = {
// year (full)
YYYY(date) {
return toDate(date).getUTCFullYear()
},
MMMM(date, locale = 'en-US') {
return new Intl.DateTimeFormat(locale, { month: 'long' }).format(toDate(date))
},
MMM(date, locale = 'en-US') {
return new Intl.DateTimeFormat(locale, { month: 'short' }).format(toDate(date))
},
dddd(date, locale = 'en-US') {
return new Intl.DateTimeFormat(locale, { weekday: 'long' }).format(toDate(date))
},
ddd(date, locale = 'en-US') {
return new Intl.DateTimeFormat(locale, { weekday: 'short' }).format(toDate(date))
},
dd(date, locale = 'en-US') {
return this.ddd(date, locale).slice(0, 2)
},
// year (2 last digits)
YY(date) {
const y = this.YYYY(date).toFixed(0)
if (y.length > 2) return y.slice(-2)
return y
},
// date num (zero-padded)
DD(date) {
const d = toDate(date).getUTCDate()
return d.toString().padStart(2, '0')
},
// month num (zero-padded)
MM(date) {
const m = toDate(date).getUTCMonth() + 1
return (m > 9 ? '' : '0') + m
},
// week of year (zero-padded)
WW(date) {
const w = getWeekOfYear(date)
return w.padStart(2, '0')
},
// unix ms timestamp
x(date) {
return toDate(date).valueOf()
},
// unix timestamp
X(date) {
return (this.x(date) / 1000).toFixed(0)
},
// date num
D(date) {
return toDate(date).getUTCDate()
},
// day of week
d(date) {
return toDate(date).getUTCDay()
},
// week of year
W: getWeekOfYear,
// month num
M(date) {
return toDate(date).getUTCMonth() + 1
},
// quarter
Q(date) {
return Math.ceil(this.M(date) / 4)
}
}
const dateTokenKeys = Object.keys(dateTokens)
function formatDate(date, format, locale) {
const result = []
while (format.length) {
let token
for (const tokenKey of dateTokenKeys) {
const match = format.match(tokenKey)
if (match && match.index === 0) {
token = match[0]
break
}
}
const mappable = !!token
if (!token) token = format.slice(0, 1)
format = format.slice(token.length)
if (mappable) token = dateTokens[token](date, locale)
result.push(token)
}
return result.join('')
}
export function dateFormatter(format, locale) {
return function formatDateFn(date) {
return formatDate(date, format, locale)
}
}