How to get ISO string from date in Javascript

Issue #552

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
type Components = {
day: number,
month: number,
year: number
}

export default class DateFormatter {
// 2018-11-11T00:00:00
static ISOStringWithoutTimeZone = (date: Date): string => {
const components = DateFormatter.format(DateFormatter.components(date))
return `${components.year}-${components.month}-${components.day}T00:00:00`
}

static format = (components: Components) => {
return {
day: `${components.day}`.padStart(2, '0'),
month: `${components.month}`.padStart(2, '0'),
year: components.year
}
}

static components = (date: Date): Components => {
return {
day: date.getDate(),
month: date.getMonth() + 1,
year: date.getFullYear()
}
}
}

Read more

Comments