/
index.ts
149 lines (127 loc) · 3.5 KB
/
index.ts
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
147
148
149
// years
const yyyy = (date: Date) => date.getFullYear().toString(); // {yyyy}
const yy = (date: Date) => date.getFullYear().toString().slice(-2); // {yy}
// months
const m = (date: Date) => (date.getMonth() + 1).toString(); // {m}
const mm = (date: Date) => {
const monthInt = date.getMonth() + 1;
return monthInt < 10 ? '0' + monthInt : monthInt.toString(); // {mm}
}
const month = (date: Date) => {
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
return months[date.getMonth()]; // {Month}
}
const mo = (date: Date) => month(date).substr(0, 3);
// days
const day = (date: Date) => date.getDate().toString();
const dd = (date: Date) => {
const d = date.getDate();
return d < 10 ? '0' + d : d.toString(); // {dd}
}
const dayWithSuffix = (date: Date) => {
const d = date.getDate();
const daySuffixes = Object.create(null);
daySuffixes.st = [1, 21, 31];
daySuffixes.nd = [2, 22];
daySuffixes.rd = [3, 23];
// loop through 'st', 'nd', and 'rd' (suffixes like '1st', '2nd', etc)
// if suffix is not found, it is 'th' (most numbers)
let suffixNotFound = true;
let suffix = '';
for (suffix in daySuffixes) {
if (daySuffixes[suffix].indexOf(d) > -1) {
suffixNotFound = false;
break;
}
}
if (suffixNotFound) suffix = 'th';
return d + suffix;
}
const weekday = (date: Date) => {
const weekdays = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];
return weekdays[date.getDay()];
}
const weekday3 = (date: Date) => weekday(date).substr(0, 3);
const weekday2 = (date: Date) => weekday(date).substr(0, 2);
const weekday1 = (date: Date) => weekday(date)[0];
// hours
const hours24 = (date: Date) => date.getHours().toString();
const hh24 = (date: Date) => {
const h = date.getHours();
return h < 10 ? '0' + h : h.toString();
}
const hours = (date: Date) => {
const h = date.getHours();
return h % 12 === 0 ? '12' : (h % 12).toString(); // {h}
}
// relies on `hours` function
const hh = (date: Date) => {
const h = date.getHours();
const n = h % 12 === 0 ? 12 : h % 12;
return n < 10 ? '0' + n : n.toString();
}
const ampm = (date: Date) => date.getHours() < 12 ? 'am' : 'pm'; // {ampm}
const AMPM = (date: Date) => date.getHours() < 12 ? 'AM' : 'PM'; // {ampm}
// minutes, seconds
const minutes = (date: Date) => {
const min = date.getMinutes();
return min < 10 ? '0' + min : min.toString();
}
const seconds = (date: Date) => {
const sec = date.getSeconds();
return sec < 10 ? '0' + sec : sec.toString();
}
interface DateMap {
[key: string]: (date: Date) => string;
}
const map: DateMap = {
'{yyyy}': yyyy,
'{yy}': yy,
'{m}': m,
'{mm}': mm,
'{Month}': month,
'{Mo}': mo,
'{d}': day,
'{dd}': dd,
'{ds}': dayWithSuffix,
'{Weekday}': weekday,
'{Day}': weekday3,
'{Dy}': weekday2,
'{D}': weekday1,
'{h24}': hours24,
'{hh24}': hh24,
'{h}': hours,
'{hh}': hh,
'{ampm}': ampm,
'{AMPM}': AMPM,
'{Minutes}': minutes,
'{Seconds}': seconds
};
const regex = /\{yyyy\}|\{yy\}|\{m\}|\{mm\}|\{Month\}|\{Mo\}|\{d\}|\{dd\}|\{ds\}|\{Weekday\}|\{Day\}|\{Dy\}|\{D\}|\{h24\}|\{hh24\}|\{h\}|\{hh\}|\{ampm\}|\{AMPM\}|\{Minutes\}|\{Seconds\}/g;
/*
formatDate('{mm}/{dd}/{yyyy}', new Date(2001, 0, 1)) => '01/01/2001'
*/
export = function formatDate(format: string, date: Date = new Date()): string {
return format.replace(regex, (match) => map[match](date));
}