1 "日期时间的工具函数"
2
3 function DateFormat(_Date, _fmt) {
4 if (!_fmt)
5 _fmt = "yyyy-MM-dd hh:mm:ss";
6
7 var o = {
8 "M+": _Date.getMonth() + 1, //月份
9 "d+": _Date.getDate(), //日
10 "h+": _Date.getHours(), //小时
11 "m+": _Date.getMinutes(), //分
12 "s+": _Date.getSeconds(), //秒
13 "q+": Math.floor((_Date.getMonth() + 3) / 3), //季度
14 "S": _Date.getMilliseconds() //毫秒
15 };
16 if (/(y+)/.test(_fmt)) _fmt = _fmt.replace(RegExp.$1, (_Date.getFullYear() + "").substr(4 - RegExp.$1.length));
17 for (var k in o)
18 if (new RegExp("(" + k + ")").test(_fmt)) _fmt = _fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
19 return _fmt;
20 }
21
22
23 ///! 时区
24 const UTC_ZONE = {
25 "UTC-0000": -0 * 3600 * 1000,
26 "UTC-0100": -1 * 3600 * 1000,
27 "UTC-0200": -2 * 3600 * 1000,
28 "UTC-0300": -3 * 3600 * 1000,
29 "UTC-0400": -4 * 3600 * 1000,
30 "UTC-0500": -5 * 3600 * 1000,
31 "UTC-0600": -6 * 3600 * 1000,
32 "UTC-0700": -7 * 3600 * 1000,
33 "UTC-0800": -8 * 3600 * 1000,
34 "UTC-0900": -9 * 3600 * 1000,
35 "UTC-1000": -10 * 3600 * 1000,
36 "UTC-1100": -11 * 3600 * 1000,
37 "UTC-1200": -12 * 3600 * 1000,
38 "UTC+0000": 0 * 3600 * 1000,
39 "UTC+0100": 1 * 3600 * 1000,
40 "UTC+0200": 2 * 3600 * 1000,
41 "UTC+0300": 3 * 3600 * 1000,
42 "UTC+0400": 4 * 3600 * 1000,
43 "UTC+0500": 5 * 3600 * 1000,
44 "UTC+0600": 6 * 3600 * 1000,
45 "UTC+0700": 7 * 3600 * 1000,
46 "UTC+0800": 8 * 3600 * 1000,
47 "UTC+0900": 9 * 3600 * 1000,
48 "UTC+1000": 10 * 3600 * 1000,
49 "UTC+1100": 11 * 3600 * 1000,
50 "UTC+1200": 12 * 3600 * 1000
51 }
52 function datetime2zone_x(_date_time, _zone) {
53
54 if (!_date_time)
55 return "N/A";
56
57 var office = UTC_ZONE[_zone];
58 var temp_date = null;
59 if (typeof _date_time == "string") {
60
61 if (_date_time == "1970-01-01 00:00:00")
62 return "N/A";
63
64 if (_date_time == "0000-00-00 00:00:00")
65 return "N/A";
66
67 if (_date_time == "null")
68 return "N/A";
69
70 if (_date_time.indexOf("UTC") < 0)
71 _date_time += " UTC+0000";
72
73 temp_date = new Date(_date_time);
74
75 }
76 else
77 temp_date = _date_time;
78
79 function to_2_str(_value) {
80 if (_value < 10)
81 return "0" + _value;
82 else
83 return _value;
84 }
85 // console.log(_date_time);
86 var temp = new Date(temp_date.getTime() + office);
87 var date_str = temp.getUTCFullYear()
88 + "-"
89 + to_2_str(temp.getUTCMonth() + 1)
90 + "-"
91 + to_2_str(temp.getUTCDate())
92 + " "
93 + to_2_str(temp.getUTCHours())
94 + ":"
95 + to_2_str(temp.getUTCMinutes())
96 + ":"
97 + to_2_str(temp.getUTCSeconds())
98 + " " + _zone;
99 return date_str;
100 }
101 function datetime2zone_0(_date_time) {
102
103 var temp_date = null;
104 if (typeof _date_time == "string") {
105 if (_date_time.indexOf("UTC") < 0)
106 return _date_time;
107 temp_date = new Date(_date_time);
108 }
109 else
110 temp_date = _date_time;
111
112 function to_2_str(_value) {
113 if (_value < 10)
114 return "0" + _value;
115 else
116 return _value;
117 }
118 temp_date = new Date(temp_date.getTime());
119
120 var date_str = temp_date.getUTCFullYear()
121 + "-"
122 + to_2_str(temp_date.getUTCMonth() + 1)
123 + "-"
124 + to_2_str(temp_date.getUTCDate())
125 + " "
126 + to_2_str(temp_date.getUTCHours())
127 + ":"
128 + to_2_str(temp_date.getUTCMinutes())
129 + ":"
130 + to_2_str(temp_date.getUTCSeconds());
131 return date_str;
132 }
133 exports.DateFormat = DateFormat;
134 exports.UTC_ZONE = UTC_ZONE;
135 exports.datetime2zone_x = datetime2zone_x;
136 exports.datetime2zone_0 = datetime2zone_0;