-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrftime.ts
More file actions
47 lines (37 loc) · 999 Bytes
/
Copy pathstrftime.ts
File metadata and controls
47 lines (37 loc) · 999 Bytes
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
interface Date {
strftime (format: string): string;
}
Date.prototype.strftime = function (format: string) {
const date = this;
return (format + "").replace(/%([a-zA-Z])/g, function(m, f) {
var formatter = formats[f];
if (typeof formatter == "function") {
return formatter.call(formats, date);
} else if (typeof formatter == "string") {
return date.strftime(formatter);
}
return f;
});
};
// Internal helper
const zeroPad = (num) => {
return (+num < 10 ? "0" : "") + num;
}
const formats = {
// formatting methods
d: function (date) {
return zeroPad(date.getDate());
},
m: function (date) {
return zeroPad(date.getMonth() +1);
},
y: function (date) {
return zeroPad(date.getYear() % 100);
},
Y: function (date) {
return date.getFullYear();
},
// Format shorthands
F: "%Y-%m-%d",
D: "%m/%d/%y"
};