-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathfmtDate.js
146 lines (125 loc) · 2.7 KB
/
fmtDate.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
FEAT_TIME,
} from './feats';
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
function slice3(str) {
return str.slice(0, 3);
}
const days3 = FEAT_TIME && days.map(slice3);
const months3 = FEAT_TIME && months.map(slice3);
const engNames = {
MMMM: months,
MMM: months3,
WWWW: days,
WWW: days3,
};
function zeroPad2(int) {
return (int < 10 ? '0' : '') + int;
}
function zeroPad3(int) {
return (int < 10 ? '00' : int < 100 ? '0' : '') + int;
}
/*
function suffix(int) {
let mod10 = int % 10;
return int + (
mod10 == 1 && int != 11 ? "st" :
mod10 == 2 && int != 12 ? "nd" :
mod10 == 3 && int != 13 ? "rd" : "th"
);
}
*/
const subs = {
// 2019
YYYY: d => d.getFullYear(),
// 19
YY: d => (d.getFullYear()+'').slice(2),
// July
MMMM: (d, names) => names.MMMM[d.getMonth()],
// Jul
MMM: (d, names) => names.MMM[d.getMonth()],
// 07
MM: d => zeroPad2(d.getMonth()+1),
// 7
M: d => d.getMonth()+1,
// 09
DD: d => zeroPad2(d.getDate()),
// 9
D: d => d.getDate(),
// Monday
WWWW: (d, names) => names.WWWW[d.getDay()],
// Mon
WWW: (d, names) => names.WWW[d.getDay()],
// 03
HH: d => zeroPad2(d.getHours()),
// 3
H: d => d.getHours(),
// 9 (12hr, unpadded)
h: d => {let h = d.getHours(); return h == 0 ? 12 : h > 12 ? h - 12 : h;},
// AM
AA: d => d.getHours() >= 12 ? 'PM' : 'AM',
// am
aa: d => d.getHours() >= 12 ? 'pm' : 'am',
// a
a: d => d.getHours() >= 12 ? 'p' : 'a',
// 09
mm: d => zeroPad2(d.getMinutes()),
// 9
m: d => d.getMinutes(),
// 09
ss: d => zeroPad2(d.getSeconds()),
// 9
s: d => d.getSeconds(),
// 374
fff: d => zeroPad3(d.getMilliseconds()),
};
export function fmtDate(tpl, names) {
names = names || engNames;
let parts = [];
let R = /\{([a-z]+)\}|[^{]+/gi, m;
while (m = R.exec(tpl))
parts.push(m[0][0] == '{' ? subs[m[1]] : m[0]);
return d => {
let out = '';
for (let i = 0; i < parts.length; i++)
out += typeof parts[i] == "string" ? parts[i] : parts[i](d, names);
return out;
}
}
const localTz = new Intl.DateTimeFormat().resolvedOptions().timeZone;
// https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone/53652131#53652131
export function tzDate(date, tz) {
let date2;
// perf optimization
if (tz == 'UTC' || tz == 'Etc/UTC')
date2 = new Date(+date + date.getTimezoneOffset() * 6e4);
else if (tz == localTz)
date2 = date;
else {
date2 = new Date(date.toLocaleString('en-US', {timeZone: tz}));
date2.setMilliseconds(date.getMilliseconds());
}
return date2;
}