-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatetimeFormatter.js
61 lines (51 loc) · 1.63 KB
/
DatetimeFormatter.js
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
export class DatetimeFormatter {
constructor() {
this.dateSeparator = '.';
this.timeSeparator = ':';
this.datetimeSeparator = ' ';
this.label = undefined;
this.showSeconds = true;
}
withDateSeparator(dateSeparator) {
this.dateSeparator = dateSeparator;
return this;
}
withTimeSeparator(timeSeparator) {
this.timeSeparator = timeSeparator;
return this;
}
withDatetimeSeparator(datetimeSeparator) {
this.datetimeSeparator = datetimeSeparator;
return this;
}
withLabel(label) {
this.label = label;
return this;
}
withShowSeconds(showSeconds) {
this.showSeconds = showSeconds;
return this;
}
formatDate(date = new Date()) {
let stringParts = [
date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
this.dateSeparator,
date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1,
this.dateSeparator,
date.getFullYear(),
this.datetimeSeparator,
date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
this.timeSeparator,
date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
];
if (this.showSeconds) {
stringParts.push(this.timeSeparator);
stringParts.push(date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
}
if (this.label) {
stringParts.push(' ');
stringParts.push(this.label);
}
return stringParts.join('');
}
}