-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatDate.js
More file actions
114 lines (97 loc) · 2.44 KB
/
formatDate.js
File metadata and controls
114 lines (97 loc) · 2.44 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
110
111
112
113
const sanitiseDate = (datetime) => {
if (!datetime) {
return new Date()
} else if (typeof datetime === 'string' || typeof datetime === 'number') {
return new Date(datetime)
}
return datetime
}
const formatDate = (datetime, targetFormat) => {
datetime = sanitiseDate(datetime)
try {
let { format } = Intl.DateTimeFormat('fr-FR', targetFormat)
return format(datetime, { timeZone: 'Europe/Paris' })
} catch (error) {
console.error('uhohohohoh', datetime, targetFormat)
return datetime
}
}
const timeFormat = {
hour: 'numeric',
minute: 'numeric',
}
const dateFormat = {
year: 'numeric',
month: 'long',
day: 'numeric',
}
const dateMonthFormat = {
month: 'long',
day: 'numeric',
}
const weekdayDateFormat = {
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long',
}
const weekdayDateMonthFormat = {
month: 'long',
day: 'numeric',
weekday: 'long',
}
const shortDateFormat = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
}
const getFormatter = (format) => (date) => formatDate(date, format)
const time = getFormatter(timeFormat)
const date = getFormatter(dateFormat)
const dateMonth = getFormatter(dateMonthFormat)
const weekdayDate = getFormatter(weekdayDateFormat)
const fifteenMinuteIndex = (datetime) => {
try {
datetime = sanitiseDate(datetime)
return datetime.getHours() * 4 + Math.floor(datetime.getMinutes() / 15)
} catch (error) {
console.log('errored with datetime', datetime, error)
return 1
}
}
const yearMonthDate = (datetime) => {
datetime = sanitiseDate(datetime)
return {
year: datetime.getFullYear(),
month: datetime.getMonth() + 1,
date: datetime.getDate(),
}
}
const routeDate = (datetime) => {
const { year, month, date } = formatAs.yearMonthDate(datetime)
return `${year}/${month}/${date}`
}
export const formatAs = {
time,
date,
dateMonth,
weekdayDate,
fifteenMinuteIndex,
yearMonthDate,
routeDate,
}
export const areSameDay = (date, ymd) => {
const { year: y1, month: m1, date: d1 } = formatAs.yearMonthDate(date)
// instead of this, assuming the ymd format, we should cover all cases so that any date-like object is valid
const { year: y2, month: m2, date: d2 } = ymd
return y1 === parseInt(y2) && m1 === parseInt(m2) && d1 === parseInt(d2)
}
export const formats = {
timeFormat,
dateFormat,
dateMonthFormat,
shortDateFormat,
weekdayDateFormat,
weekdayDateMonthFormat,
}
export default formatDate