-
Notifications
You must be signed in to change notification settings - Fork 0
/
unicode-technical-report-35.js
77 lines (75 loc) · 2.92 KB
/
unicode-technical-report-35.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
"use strict";
function format(date, str) {
const regex = /[a-zA-Z+]{1,4}/g
const regexArr = str.match(regex)
const monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
const weekNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
for (const ele of regexArr) {
switch (ele) {
case 'y':
str = str.replace(ele, Math.abs(date.getFullYear()))
break;
case 'yyyy':
str = str.replace(ele, Math.abs(date.getFullYear()).toString().padStart(4, '0'))
break;
case 'G':
str = str.replace(ele, date.getFullYear() > 0? "AD":"BC")
break;
case 'GGGG':
str = str.replace(ele, date.getFullYear() > 0? "Anno Domini":"Before Christ")
break;
case 'M':
str = str.replace(ele, date.getMonth()+1)
break;
case 'MM':
str = str.replace(ele, (date.getMonth()+1).toString().padStart(2, '0'))
break;
case 'MMM':
str = str.replace(ele, monthNames[date.getMonth()].slice(0, 3))
break;
case 'MMMM':
str = str.replace(ele, monthNames[date.getMonth()])
break;
case 'd':
str = str.replace(ele, date.getDate())
break;
case 'dd':
str = str.replace(ele, date.getDate().toString().padStart(2, '0'))
break;
case 'E':
str = str.replace(ele, weekNames[date.getDay()].slice(0, 3))
break;
case 'EEEE':
str = str.replace(ele, weekNames[date.getDay()])
break;
case 'h':
str = str.replace(ele, date.getHours() % 12)
break;
case 'hh':
str = str.replace(ele, (date.getHours() % 12).toString().padStart(2, '0'))
break;
case 'm':
str = str.replace(ele, date.getMinutes())
break;
case 'mm':
str = str.replace(ele, date.getMinutes().toString().padStart(2, '0'))
break;
case 's':
str = str.replace(ele, date.getSeconds())
break;
case 'ss':
str = str.replace(ele, date.getSeconds().toString().padStart(2, '0'))
break;
case 'H':
str = str.replace(ele, date.getHours())
break;
case 'HH':
str = str.replace(ele, date.getHours().toString().padStart(2, '0'))
break;
case 'a':
str = str.replace(ele, date.getHours() < 12? `AM`:`PM`)
break;
}
}
return str
}