tools-min-ns 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -0
- package/esm/array/ArrayUtil.d.ts +109 -0
- package/esm/array/ArrayUtil.js +242 -0
- package/esm/array/TreeUtil.d.ts +151 -0
- package/esm/array/TreeUtil.js +412 -0
- package/esm/base/BaseUtil.d.ts +91 -0
- package/esm/base/BaseUtil.js +148 -0
- package/esm/browser/BrowserUtil.d.ts +57 -0
- package/esm/browser/BrowserUtil.js +337 -0
- package/esm/browser/fingerprint.d.ts +120 -0
- package/esm/browser/fingerprint.js +1469 -0
- package/esm/check/CheckUtil.d.ts +110 -0
- package/esm/check/CheckUtil.js +465 -0
- package/esm/common.d.ts +43 -0
- package/esm/common.js +44 -0
- package/esm/config.d.ts +7 -0
- package/esm/config.js +7 -0
- package/esm/cookie/CookieUtil.d.ts +56 -0
- package/esm/cookie/CookieUtil.js +12 -0
- package/esm/cookie/cookie.d.ts +2 -0
- package/esm/cookie/cookie.js +104 -0
- package/esm/date/DateUtil.d.ts +261 -0
- package/esm/date/DateUtil.js +763 -0
- package/esm/env/EnvUtil.d.ts +27 -0
- package/esm/env/EnvUtil.js +29 -0
- package/esm/env/env.d.ts +41 -0
- package/esm/env/env.js +106 -0
- package/esm/func/FunctionUtil.d.ts +77 -0
- package/esm/func/FunctionUtil.js +215 -0
- package/esm/index.d.ts +19 -0
- package/esm/index.js +19 -0
- package/esm/number/NumberUtil.d.ts +96 -0
- package/esm/number/NumberUtil.js +256 -0
- package/esm/object/ObjectUtil.d.ts +41 -0
- package/esm/object/ObjectUtil.js +158 -0
- package/esm/pwd/PasswordUtil.d.ts +33 -0
- package/esm/pwd/PasswordUtil.js +68 -0
- package/esm/pwd/crypto.d.ts +6 -0
- package/esm/pwd/crypto.js +1824 -0
- package/esm/request/RequestUtil.d.ts +52 -0
- package/esm/request/RequestUtil.js +53 -0
- package/esm/request/jsonp.d.ts +18 -0
- package/esm/request/jsonp.js +73 -0
- package/esm/request/mgop/env.d.ts +7 -0
- package/esm/request/mgop/env.js +15 -0
- package/esm/request/mgop/index.d.ts +3 -0
- package/esm/request/mgop/index.js +276 -0
- package/esm/request/mgop/responseCode.d.ts +20 -0
- package/esm/request/mgop/responseCode.js +37 -0
- package/esm/request/mgop/type.d.ts +34 -0
- package/esm/request/mgop/type.js +1 -0
- package/esm/request/uniRquest/index.d.ts +2 -0
- package/esm/request/uniRquest/index.js +66 -0
- package/esm/request/uniRquest/miniapp/index.d.ts +2 -0
- package/esm/request/uniRquest/miniapp/index.js +29 -0
- package/esm/request/uniRquest/miniapp-bytedance/index.d.ts +2 -0
- package/esm/request/uniRquest/miniapp-bytedance/index.js +41 -0
- package/esm/request/uniRquest/types.d.ts +65 -0
- package/esm/request/uniRquest/types.js +36 -0
- package/esm/request/uniRquest/utils.d.ts +9 -0
- package/esm/request/uniRquest/utils.js +69 -0
- package/esm/request/uniRquest/web/index.d.ts +2 -0
- package/esm/request/uniRquest/web/index.js +85 -0
- package/esm/request/uniRquest/wechat-miniprogram/index.d.ts +2 -0
- package/esm/request/uniRquest/wechat-miniprogram/index.js +41 -0
- package/esm/request/uniRquest/weex/index.d.ts +14 -0
- package/esm/request/uniRquest/weex/index.js +59 -0
- package/esm/storage/StorageUtil.d.ts +19 -0
- package/esm/storage/StorageUtil.js +75 -0
- package/esm/string/StringUtil.d.ts +392 -0
- package/esm/string/StringUtil.js +556 -0
- package/esm/system/SystemUtil.d.ts +46 -0
- package/esm/system/SystemUtil.js +122 -0
- package/esm/transfer/MapTransferUtil.d.ts +54 -0
- package/esm/transfer/MapTransferUtil.js +160 -0
- package/esm/transfer/TransferUtil.d.ts +98 -0
- package/esm/transfer/TransferUtil.js +323 -0
- package/esm/types.d.ts +25 -0
- package/esm/types.js +1 -0
- package/esm/url/UrlUtil.d.ts +30 -0
- package/esm/url/UrlUtil.js +85 -0
- package/lib/array/ArrayUtil.d.ts +109 -0
- package/lib/array/ArrayUtil.js +252 -0
- package/lib/array/TreeUtil.d.ts +151 -0
- package/lib/array/TreeUtil.js +422 -0
- package/lib/base/BaseUtil.d.ts +91 -0
- package/lib/base/BaseUtil.js +153 -0
- package/lib/browser/BrowserUtil.d.ts +57 -0
- package/lib/browser/BrowserUtil.js +347 -0
- package/lib/browser/fingerprint.d.ts +120 -0
- package/lib/browser/fingerprint.js +1500 -0
- package/lib/check/CheckUtil.d.ts +110 -0
- package/lib/check/CheckUtil.js +475 -0
- package/lib/common.d.ts +43 -0
- package/lib/common.js +50 -0
- package/lib/config.d.ts +7 -0
- package/lib/config.js +12 -0
- package/lib/cookie/CookieUtil.d.ts +56 -0
- package/lib/cookie/CookieUtil.js +22 -0
- package/lib/cookie/cookie.d.ts +2 -0
- package/lib/cookie/cookie.js +109 -0
- package/lib/date/DateUtil.d.ts +261 -0
- package/lib/date/DateUtil.js +773 -0
- package/lib/env/EnvUtil.d.ts +27 -0
- package/lib/env/EnvUtil.js +39 -0
- package/lib/env/env.d.ts +41 -0
- package/lib/env/env.js +112 -0
- package/lib/func/FunctionUtil.d.ts +77 -0
- package/lib/func/FunctionUtil.js +225 -0
- package/lib/index.d.ts +19 -0
- package/lib/index.js +144 -0
- package/lib/number/NumberUtil.d.ts +96 -0
- package/lib/number/NumberUtil.js +266 -0
- package/lib/object/ObjectUtil.d.ts +41 -0
- package/lib/object/ObjectUtil.js +168 -0
- package/lib/pwd/PasswordUtil.d.ts +33 -0
- package/lib/pwd/PasswordUtil.js +78 -0
- package/lib/pwd/crypto.d.ts +6 -0
- package/lib/pwd/crypto.js +1829 -0
- package/lib/request/RequestUtil.d.ts +52 -0
- package/lib/request/RequestUtil.js +94 -0
- package/lib/request/jsonp.d.ts +18 -0
- package/lib/request/jsonp.js +80 -0
- package/lib/request/mgop/env.d.ts +7 -0
- package/lib/request/mgop/env.js +21 -0
- package/lib/request/mgop/index.d.ts +3 -0
- package/lib/request/mgop/index.js +289 -0
- package/lib/request/mgop/responseCode.d.ts +20 -0
- package/lib/request/mgop/responseCode.js +42 -0
- package/lib/request/mgop/type.d.ts +34 -0
- package/lib/request/mgop/type.js +5 -0
- package/lib/request/uniRquest/index.d.ts +2 -0
- package/lib/request/uniRquest/index.js +77 -0
- package/lib/request/uniRquest/miniapp/index.d.ts +2 -0
- package/lib/request/uniRquest/miniapp/index.js +35 -0
- package/lib/request/uniRquest/miniapp-bytedance/index.d.ts +2 -0
- package/lib/request/uniRquest/miniapp-bytedance/index.js +47 -0
- package/lib/request/uniRquest/types.d.ts +65 -0
- package/lib/request/uniRquest/types.js +42 -0
- package/lib/request/uniRquest/utils.d.ts +9 -0
- package/lib/request/uniRquest/utils.js +83 -0
- package/lib/request/uniRquest/web/index.d.ts +2 -0
- package/lib/request/uniRquest/web/index.js +91 -0
- package/lib/request/uniRquest/wechat-miniprogram/index.d.ts +2 -0
- package/lib/request/uniRquest/wechat-miniprogram/index.js +47 -0
- package/lib/request/uniRquest/weex/index.d.ts +14 -0
- package/lib/request/uniRquest/weex/index.js +65 -0
- package/lib/storage/StorageUtil.d.ts +19 -0
- package/lib/storage/StorageUtil.js +85 -0
- package/lib/string/StringUtil.d.ts +392 -0
- package/lib/string/StringUtil.js +566 -0
- package/lib/system/SystemUtil.d.ts +46 -0
- package/lib/system/SystemUtil.js +132 -0
- package/lib/transfer/MapTransferUtil.d.ts +54 -0
- package/lib/transfer/MapTransferUtil.js +165 -0
- package/lib/transfer/TransferUtil.d.ts +98 -0
- package/lib/transfer/TransferUtil.js +333 -0
- package/lib/types.d.ts +25 -0
- package/lib/types.js +5 -0
- package/lib/url/UrlUtil.d.ts +30 -0
- package/lib/url/UrlUtil.js +95 -0
- package/package.json +56 -0
|
@@ -0,0 +1,773 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __read = this && this.__read || function (o, n) {
|
|
4
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
5
|
+
if (!m) return o;
|
|
6
|
+
var i = m.call(o),
|
|
7
|
+
r,
|
|
8
|
+
ar = [],
|
|
9
|
+
e;
|
|
10
|
+
try {
|
|
11
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
e = {
|
|
14
|
+
error: error
|
|
15
|
+
};
|
|
16
|
+
} finally {
|
|
17
|
+
try {
|
|
18
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
19
|
+
} finally {
|
|
20
|
+
if (e) throw e.error;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = this && this.__importDefault || function (mod) {
|
|
26
|
+
return mod && mod.__esModule ? mod : {
|
|
27
|
+
"default": mod
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", {
|
|
31
|
+
value: true
|
|
32
|
+
});
|
|
33
|
+
/**
|
|
34
|
+
* 日期工具
|
|
35
|
+
* @description
|
|
36
|
+
* @author nanshen
|
|
37
|
+
* @creat 2021-09-15 10:31:14
|
|
38
|
+
*/
|
|
39
|
+
var BaseUtil_1 = __importDefault(require("../base/BaseUtil"));
|
|
40
|
+
var DateUtil;
|
|
41
|
+
(function (DateUtil) {
|
|
42
|
+
/**
|
|
43
|
+
* 将 字符串/时间戳 解析成日期对象
|
|
44
|
+
* @param {Date | String | Number} date 输入的日期字符串,如'2014-09-13',或者时间戳
|
|
45
|
+
* @param {String} format 字符串格式,默认'yyyy-MM-dd',支持如下:y、M、d、H、m、s、S
|
|
46
|
+
* @returns {Date} 解析后的Date对象
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* parseDate('2018-11-26'); => new Date(2018, 10, 26)
|
|
50
|
+
* parseDate('11/26/2018', 'MM/dd/yyyy') => new Date(2018, 10, 26)
|
|
51
|
+
* parseDate('2018-11-26 13:28:43', 'yyyy-MM-dd HH:mm:ss') => new Date(2018, 10, 26, 13, 28, 43)
|
|
52
|
+
* parseDate('2018年11月26日', 'yyyy年MM月dd日') => new Date(2018, 10, 26)
|
|
53
|
+
* parseDate('2018年11月26日 11时40分', 'yyyy年MM月dd日 HH时mm分') => new Date(2018, 10, 26, 11, 40)
|
|
54
|
+
*/
|
|
55
|
+
DateUtil.parseDate = function (date, format) {
|
|
56
|
+
if (format === void 0) {
|
|
57
|
+
format = 'yyyy-MM-dd';
|
|
58
|
+
}
|
|
59
|
+
// 如果传入的是日期对象,则不需解析,直接返回
|
|
60
|
+
if (BaseUtil_1.default.isDate(date)) {
|
|
61
|
+
return date;
|
|
62
|
+
}
|
|
63
|
+
// 如果传入的是数字,则被认为是时间戳
|
|
64
|
+
if (BaseUtil_1.default.isNumber(date)) {
|
|
65
|
+
return new Date(date);
|
|
66
|
+
}
|
|
67
|
+
// 如果不是字符串,则表示类型不支持
|
|
68
|
+
if (!BaseUtil_1.default.isString(date)) {
|
|
69
|
+
return new Date();
|
|
70
|
+
}
|
|
71
|
+
var dateStr = date;
|
|
72
|
+
var rule = [];
|
|
73
|
+
var sIndex = 0;
|
|
74
|
+
var dates = [];
|
|
75
|
+
dateFormatRules.forEach(function (item) {
|
|
76
|
+
for (var index = 0, rules = item.rules, len = rules.length; index < len; index++) {
|
|
77
|
+
rule = rules[index];
|
|
78
|
+
sIndex = format.indexOf("".concat(rule[0]));
|
|
79
|
+
if (sIndex !== -1) {
|
|
80
|
+
dates.push(parseFloat(dateStr.substring(sIndex, sIndex + Number(rule[1]))) + (item.offset || 0));
|
|
81
|
+
// 如果匹配到规则中的第一条,则退出
|
|
82
|
+
break;
|
|
83
|
+
} else if (index === len - 1) {
|
|
84
|
+
// 如果是规则中的最后一条,说明format格式中没有出现该字段
|
|
85
|
+
dates.push(0);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return new Date(dates[0] || 0, dates[1] || 0, dates[2], dates[3], dates[4], dates[5], dates[6]);
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* 将日期格式化成指定格式的字符串
|
|
93
|
+
* @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
|
|
94
|
+
* @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
|
|
95
|
+
* @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
|
|
96
|
+
* @example
|
|
97
|
+
* formatDate(); => 2016-09-02 13:17:13
|
|
98
|
+
* formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
|
|
99
|
+
* formatDate(new Date(), 'yyyy-MM-dd'); => 2016-09-02
|
|
100
|
+
* formatDate(new Date(), 'yyyy-MM-dd 第q季度 www HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
|
|
101
|
+
* formatDate(1472793615764); => 2016-09-02 13:20:15
|
|
102
|
+
*/
|
|
103
|
+
DateUtil.formatDate = function (date, format) {
|
|
104
|
+
if (date === void 0) {
|
|
105
|
+
date = new Date();
|
|
106
|
+
}
|
|
107
|
+
if (format === void 0) {
|
|
108
|
+
format = 'yyyy-MM-dd HH:mm:ss';
|
|
109
|
+
}
|
|
110
|
+
if (BaseUtil_1.default.isString(date)) {
|
|
111
|
+
return DateUtil.formatDate(DateUtil.parseDate(date), format);
|
|
112
|
+
}
|
|
113
|
+
if (BaseUtil_1.default.isDate(date)) {
|
|
114
|
+
// nop
|
|
115
|
+
} else if (BaseUtil_1.default.isNumber(date)) {
|
|
116
|
+
date = new Date(date);
|
|
117
|
+
} else {
|
|
118
|
+
return 'invalid date';
|
|
119
|
+
}
|
|
120
|
+
date = date;
|
|
121
|
+
var obj = {
|
|
122
|
+
y: date.getFullYear(),
|
|
123
|
+
M: date.getMonth() + 1,
|
|
124
|
+
d: date.getDate(),
|
|
125
|
+
q: Math.floor((date.getMonth() + 3) / 3),
|
|
126
|
+
w: date.getDay(),
|
|
127
|
+
H: date.getHours(),
|
|
128
|
+
h: date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
|
|
129
|
+
m: date.getMinutes(),
|
|
130
|
+
s: date.getSeconds(),
|
|
131
|
+
S: date.getMilliseconds() // 毫秒
|
|
132
|
+
};
|
|
133
|
+
var week = ['天', '一', '二', '三', '四', '五', '六'];
|
|
134
|
+
var _loop_1 = function _loop_1(i) {
|
|
135
|
+
format = format.replace(new RegExp(i + '+', 'g'), function (m) {
|
|
136
|
+
var val = "".concat(obj[i]);
|
|
137
|
+
if (i === 'w') {
|
|
138
|
+
return (m.length > 2 ? '星期' : '周') + week[Number(val)];
|
|
139
|
+
}
|
|
140
|
+
for (var j = 0, len = val.length; j < m.length - len; j++) {
|
|
141
|
+
val = '0' + val;
|
|
142
|
+
}
|
|
143
|
+
return m.length === 1 ? val : val.substring(val.length - m.length);
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
for (var i in obj) {
|
|
147
|
+
_loop_1(i);
|
|
148
|
+
}
|
|
149
|
+
return format;
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* 将一个日期格式化成友好格式,比如,1分钟以内的返回“刚刚”,
|
|
153
|
+
* 当天的返回时分,当年的返回月日,否则,返回年月日
|
|
154
|
+
* @param {Date | number | string} date 如: new Date('2017-11-11')
|
|
155
|
+
* @returns {string} 解析后的友好化日期格式
|
|
156
|
+
* @example
|
|
157
|
+
* formatDateToFriendly() => 刚刚
|
|
158
|
+
*/
|
|
159
|
+
DateUtil.formatDateToFriendly = function (date) {
|
|
160
|
+
if (date === void 0) {
|
|
161
|
+
date = new Date();
|
|
162
|
+
}
|
|
163
|
+
if (BaseUtil_1.default.isString(date)) {
|
|
164
|
+
date = DateUtil.parseDate(date, 'yyyy-MM-dd HH:mm:ss');
|
|
165
|
+
} else {
|
|
166
|
+
date = BaseUtil_1.default.isNumber(date) ? new Date(date) : date;
|
|
167
|
+
}
|
|
168
|
+
date = date;
|
|
169
|
+
var now = new Date();
|
|
170
|
+
if (now.getTime() - date.getTime() < 60 * 1000) {
|
|
171
|
+
// 1分钟以内视作“刚刚”
|
|
172
|
+
return '刚刚';
|
|
173
|
+
}
|
|
174
|
+
var temp = DateUtil.formatDate(date, 'yyyy年M月d日');
|
|
175
|
+
if (temp === DateUtil.formatDate(now, 'yyyy年M月d日')) {
|
|
176
|
+
return DateUtil.formatDate(date, 'HH:mm');
|
|
177
|
+
}
|
|
178
|
+
if (date.getFullYear() === now.getFullYear()) {
|
|
179
|
+
return DateUtil.formatDate(date, 'M月d日');
|
|
180
|
+
}
|
|
181
|
+
return temp;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* 将一个日期格式化成友好格式,比如,当天返回时间,昨天返回 昨天 xx:xx ,本周返回 周x xx:xx 本年 xx月xx日 xx:xx 不是本年 xx年xx月xx日 xx:xx,
|
|
185
|
+
* @param {Date | number | string} date 如: new Date('2017-11-11')
|
|
186
|
+
* @returns {string} 解析后的友好化日期格式
|
|
187
|
+
* @example
|
|
188
|
+
* formatDateToFriendly2() => 2017年11月11日
|
|
189
|
+
*/
|
|
190
|
+
function formatDateToFriendly2(date) {
|
|
191
|
+
if (date === void 0) {
|
|
192
|
+
date = new Date();
|
|
193
|
+
}
|
|
194
|
+
if (BaseUtil_1.default.isString(date)) {
|
|
195
|
+
date = DateUtil.parseDate(date, 'yyyy-MM-dd HH:mm:ss');
|
|
196
|
+
} else {
|
|
197
|
+
date = BaseUtil_1.default.isNumber(date) ? new Date(date) : date;
|
|
198
|
+
}
|
|
199
|
+
var today = new Date();
|
|
200
|
+
var yesterday = new Date(today);
|
|
201
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
202
|
+
var weekDays = ['日', '一', '二', '三', '四', '五', '六'];
|
|
203
|
+
var _time = date.toTimeString().split(' ')[0];
|
|
204
|
+
var time = _time.split(':').slice(0, 2).join(':');
|
|
205
|
+
if (date.toDateString() === today.toDateString()) {
|
|
206
|
+
return time;
|
|
207
|
+
} else if (date.toDateString() === yesterday.toDateString()) {
|
|
208
|
+
return "\u6628\u5929 ".concat(time);
|
|
209
|
+
} else if (isCurrentWeek(date)) {
|
|
210
|
+
return "\u5468".concat(weekDays[date.getDay()], " ").concat(time);
|
|
211
|
+
} else if (date.getFullYear() === today.getFullYear()) {
|
|
212
|
+
return "".concat(date.getMonth() + 1, "\u6708").concat(date.getDate(), "\u65E5 ").concat(time);
|
|
213
|
+
} else {
|
|
214
|
+
return "".concat(date.getFullYear(), "\u5E74").concat(date.getMonth() + 1, "\u6708").concat(date.getDate(), "\u65E5 ").concat(time);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
DateUtil.formatDateToFriendly2 = formatDateToFriendly2;
|
|
218
|
+
/**
|
|
219
|
+
* 判断是否为本周
|
|
220
|
+
* @param date
|
|
221
|
+
* @returns
|
|
222
|
+
*/
|
|
223
|
+
function isCurrentWeek(date) {
|
|
224
|
+
if (date === void 0) {
|
|
225
|
+
date = new Date();
|
|
226
|
+
}
|
|
227
|
+
var pastTime = date = BaseUtil_1.default.isNumber(date) ? new Date(date) : date;
|
|
228
|
+
var today = new Date(new Date().toLocaleDateString());
|
|
229
|
+
var day = today.getDay();
|
|
230
|
+
day = day == 0 ? 7 : day;
|
|
231
|
+
var oneDayTime = 60 * 60 * 24 * 1000;
|
|
232
|
+
var monday = new Date(today.getTime() - oneDayTime * (day - 1));
|
|
233
|
+
var nextMonday = new Date(today.getTime() + oneDayTime * (8 - day));
|
|
234
|
+
return monday.getTime() <= pastTime.getTime() && nextMonday.getTime() > pastTime.getTime();
|
|
235
|
+
}
|
|
236
|
+
DateUtil.isCurrentWeek = isCurrentWeek;
|
|
237
|
+
/**
|
|
238
|
+
* 获取日期对应的时间戳
|
|
239
|
+
* @param {Date | String} date 日期对象/日期字符串
|
|
240
|
+
* @param {String} format 字符串模板(date参数为字符串时有效)
|
|
241
|
+
* @returns {Number} 时间戳(单位:毫秒) 如果date参数不合法,则返回-1
|
|
242
|
+
* @example
|
|
243
|
+
* timestamp() => 1610625880136
|
|
244
|
+
*/
|
|
245
|
+
DateUtil.timestamp = function (date, format) {
|
|
246
|
+
if (date === void 0) {
|
|
247
|
+
date = new Date();
|
|
248
|
+
}
|
|
249
|
+
if (format === void 0) {
|
|
250
|
+
format = 'yyyy-MM-dd';
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
date = DateUtil.parseDate(date, format);
|
|
254
|
+
return date.getTime();
|
|
255
|
+
} catch (e) {
|
|
256
|
+
return -1;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* 计算指定日期往前推指定时间
|
|
261
|
+
* @param {Date | string | number} date 初始日期,支持 Date 对象、日期字符串(需配合format参数)、时间戳
|
|
262
|
+
* @param {IDateDiff} diff 日期差值,支持年、月、日、周、时、分、秒、毫秒
|
|
263
|
+
* @param {string} format 日期格式字符串
|
|
264
|
+
* @example
|
|
265
|
+
* dateBefore(new Date(),{year:1}) => Tue Jan 14 2020 20:21:26 GMT+0800 (中国标准时间)
|
|
266
|
+
*/
|
|
267
|
+
DateUtil.dateBefore = function (date, diff, format) {
|
|
268
|
+
if (format === void 0) {
|
|
269
|
+
format = 'yyyy-MM-dd';
|
|
270
|
+
}
|
|
271
|
+
if (BaseUtil_1.default.isString(date)) {
|
|
272
|
+
date = DateUtil.parseDate(date, format);
|
|
273
|
+
} else if (BaseUtil_1.default.isNumber(date)) {
|
|
274
|
+
date = new Date(date);
|
|
275
|
+
} else if (!BaseUtil_1.default.isDate(date)) {
|
|
276
|
+
// 如果传入的初始时间无法识别,则采用当前时间
|
|
277
|
+
date = new Date();
|
|
278
|
+
}
|
|
279
|
+
date = date;
|
|
280
|
+
return _dateDiff(date, diff, 'BEFORE');
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* 计算指定日期往后推指定时间
|
|
284
|
+
* @param {Date | string | number} date 初始日期,支持 Date 对象、日期字符串(需配合format参数)、时间戳
|
|
285
|
+
* @param {IDateDiff} diff 日期差值,支持年、月、日、周、时、分、秒、毫秒
|
|
286
|
+
* @param {string} format 日期格式字符串
|
|
287
|
+
* @example
|
|
288
|
+
* dateAfter(new Date(),{year:1}) => Tue Jan 14 2022 20:21:26 GMT+0800 (中国标准时间)
|
|
289
|
+
*/
|
|
290
|
+
DateUtil.dateAfter = function (date, diff, format) {
|
|
291
|
+
if (format === void 0) {
|
|
292
|
+
format = 'yyyy-MM-dd';
|
|
293
|
+
}
|
|
294
|
+
if (BaseUtil_1.default.isString(date)) {
|
|
295
|
+
date = DateUtil.parseDate(date, format);
|
|
296
|
+
} else if (BaseUtil_1.default.isNumber(date)) {
|
|
297
|
+
date = new Date(date);
|
|
298
|
+
} else if (!BaseUtil_1.default.isDate(date)) {
|
|
299
|
+
// 如果传入的初始时间无法识别,则采用当前时间
|
|
300
|
+
date = new Date();
|
|
301
|
+
}
|
|
302
|
+
date = date;
|
|
303
|
+
return _dateDiff(date, diff, 'AFTER');
|
|
304
|
+
};
|
|
305
|
+
/**
|
|
306
|
+
* 内部日期计算函数
|
|
307
|
+
* @param {Date} date 初始日期
|
|
308
|
+
* @param {IDateDiff} diff 日期差值,支持年、月、日、周、时、分、秒、毫秒
|
|
309
|
+
* @param {DiffDirection} direction 计算的方向
|
|
310
|
+
*/
|
|
311
|
+
var _dateDiff = function _dateDiff(date, diff, direction) {
|
|
312
|
+
var result = new Date();
|
|
313
|
+
// 将天单位以下的计算出 millsecond
|
|
314
|
+
var ms = (diff.week || 0) * 7 * 24 * 60 * 60 * 1000 + (diff.day || 0) * 24 * 60 * 60 * 1000 + (diff.hour || 0) * 60 * 60 * 1000 + (diff.minute || 0) * 60 * 1000 + (diff.second || 0) * 1000 + (diff.millsecond || 0);
|
|
315
|
+
if (direction === 'BEFORE') {
|
|
316
|
+
result.setTime(date.getTime() - ms);
|
|
317
|
+
} else {
|
|
318
|
+
result.setTime(date.getTime() + ms);
|
|
319
|
+
}
|
|
320
|
+
// 处理 month
|
|
321
|
+
var n = result.getDate();
|
|
322
|
+
result.setDate(1);
|
|
323
|
+
if (direction === 'BEFORE') {
|
|
324
|
+
result.setMonth(result.getMonth() - (diff.month || 0));
|
|
325
|
+
} else {
|
|
326
|
+
result.setMonth(result.getMonth() + (diff.month || 0));
|
|
327
|
+
}
|
|
328
|
+
result.setDate(Math.min(n, DateUtil.getDaysInMonth(result.getFullYear(), result.getMonth())));
|
|
329
|
+
// 处理 year
|
|
330
|
+
var m = result.getDate();
|
|
331
|
+
result.setDate(1);
|
|
332
|
+
if (direction === 'BEFORE') {
|
|
333
|
+
result.setFullYear(result.getFullYear() - (diff.year || 0));
|
|
334
|
+
} else {
|
|
335
|
+
result.setFullYear(result.getFullYear() + (diff.year || 0));
|
|
336
|
+
}
|
|
337
|
+
result.setDate(Math.min(m, DateUtil.getDaysInMonth(result.getFullYear(), result.getMonth())));
|
|
338
|
+
return result;
|
|
339
|
+
};
|
|
340
|
+
/**
|
|
341
|
+
* 判断是否为闰年
|
|
342
|
+
* @param {Number} year 年份
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* isLeapYear(1900) = false
|
|
346
|
+
* isLeapYear(2000) = true
|
|
347
|
+
* isLeapYear(2001) = false
|
|
348
|
+
* isLeapYear(2004) = true
|
|
349
|
+
*/
|
|
350
|
+
DateUtil.isLeapYear = function (year) {
|
|
351
|
+
return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
352
|
+
};
|
|
353
|
+
/**
|
|
354
|
+
* 计算在给定的年份中,给定的月份有多少天(会自动判断是否为闰年)
|
|
355
|
+
* @param {Number} year 年份
|
|
356
|
+
* @param {Number} month 月份 (0-11)
|
|
357
|
+
* @example
|
|
358
|
+
* getDaysInMonth(2012,2) => 31
|
|
359
|
+
*/
|
|
360
|
+
DateUtil.getDaysInMonth = function (year, month) {
|
|
361
|
+
return [31, DateUtil.isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
|
|
362
|
+
};
|
|
363
|
+
/**
|
|
364
|
+
* 获取时间的开头
|
|
365
|
+
* @param date 日期
|
|
366
|
+
* @param mode 时间维度 可以使用 Year Month Day Week Hour Minute Second
|
|
367
|
+
* @param format 字符串模板
|
|
368
|
+
* @example
|
|
369
|
+
* getDateStart(new Date(),'YEAR') => Fri Jan 01 2021 00:00:00 GMT+0800 (中国标准时间)
|
|
370
|
+
*/
|
|
371
|
+
DateUtil.getDateStart = function (date, mode, format) {
|
|
372
|
+
if (format === void 0) {
|
|
373
|
+
format = 'yyyy-MM-dd';
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
date = DateUtil.parseDate(date, format);
|
|
377
|
+
} catch (_a) {
|
|
378
|
+
date = new Date();
|
|
379
|
+
}
|
|
380
|
+
var parts = _parseDatePart(date);
|
|
381
|
+
switch (mode) {
|
|
382
|
+
case 'YEAR':
|
|
383
|
+
return new Date(parts.Year, 0, 1);
|
|
384
|
+
case 'MONTH':
|
|
385
|
+
return new Date(parts.Year, parts.Month, 1);
|
|
386
|
+
case 'WEEK':
|
|
387
|
+
{
|
|
388
|
+
var year = date.getFullYear();
|
|
389
|
+
var month = date.getMonth() + 1;
|
|
390
|
+
var dd = date.getDate(); // 获取日期
|
|
391
|
+
var day = date.getDay(); // 获取周几,0-6,0表示周日
|
|
392
|
+
var _day = day == 0 ? 7 : day;
|
|
393
|
+
// 获取周一日期
|
|
394
|
+
var startDate = new Date(year, month, dd - _day + 1);
|
|
395
|
+
return startDate;
|
|
396
|
+
}
|
|
397
|
+
case 'DATE':
|
|
398
|
+
return new Date(parts.Year, parts.Month, parts.Date);
|
|
399
|
+
case 'HOUR':
|
|
400
|
+
return new Date(parts.Year, parts.Month, parts.Date, parts.Hour);
|
|
401
|
+
case 'MINUTE':
|
|
402
|
+
return new Date(parts.Year, parts.Month, parts.Date, parts.Hour, parts.Minute);
|
|
403
|
+
case 'SECOND':
|
|
404
|
+
return new Date(parts.Year, parts.Month, parts.Date, parts.Hour, parts.Minute, parts.Second);
|
|
405
|
+
default:
|
|
406
|
+
throw TypeError('mode is invalid');
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
/**
|
|
410
|
+
* 获取时间的结尾
|
|
411
|
+
* @param date 日期
|
|
412
|
+
* @param mode 时间维度 可以使用 Year Month Date Week Hour Minute Second
|
|
413
|
+
* @param format 字符串模板
|
|
414
|
+
* @example
|
|
415
|
+
* getDateEnd(new Date(),TimeDimension.Year) => Fri Dec 31 2021 23:59:59 GMT+0800 (中国标准时间)
|
|
416
|
+
*/
|
|
417
|
+
DateUtil.getDateEnd = function (date, mode, format) {
|
|
418
|
+
if (format === void 0) {
|
|
419
|
+
format = 'yyyy-MM-dd';
|
|
420
|
+
}
|
|
421
|
+
try {
|
|
422
|
+
date = DateUtil.parseDate(date, format);
|
|
423
|
+
} catch (_a) {
|
|
424
|
+
date = new Date();
|
|
425
|
+
}
|
|
426
|
+
var parts = _parseDatePart(date);
|
|
427
|
+
switch (mode) {
|
|
428
|
+
case 'YEAR':
|
|
429
|
+
return DateUtil.dateBefore(new Date(parts.Year + 1, 0, 1), {
|
|
430
|
+
millsecond: 1
|
|
431
|
+
});
|
|
432
|
+
case 'MONTH':
|
|
433
|
+
return new Date(parts.Year, parts.Month, DateUtil.getDaysInMonth(parts.Year, parts.Month), 23, 59, 59, 999);
|
|
434
|
+
case 'WEEK':
|
|
435
|
+
var year = date.getFullYear();
|
|
436
|
+
var month = date.getMonth() + 1;
|
|
437
|
+
var dd = date.getDate(); // 获取日期
|
|
438
|
+
var day = date.getDay(); // 获取周几,0-6,0表示周日
|
|
439
|
+
var _day = day == 0 ? 7 : day;
|
|
440
|
+
// 获取周一日期
|
|
441
|
+
var startDate = new Date(year, month, dd - _day + 1);
|
|
442
|
+
// 获取周日日期
|
|
443
|
+
var endDate = new Date(new Date(startDate).getTime() + 6 * 24 * 3600 * 1000);
|
|
444
|
+
return endDate;
|
|
445
|
+
case 'DATE':
|
|
446
|
+
return new Date(parts.Year, parts.Month, parts.Date, 23, 59, 59, 999);
|
|
447
|
+
case 'HOUR':
|
|
448
|
+
return new Date(parts.Year, parts.Month, parts.Date, parts.Hour, 59, 59, 999);
|
|
449
|
+
case 'MINUTE':
|
|
450
|
+
return new Date(parts.Year, parts.Month, parts.Date, parts.Hour, parts.Minute, 59, 999);
|
|
451
|
+
case 'SECOND':
|
|
452
|
+
return new Date(parts.Year, parts.Month, parts.Date, parts.Hour, parts.Minute, parts.Second, 999);
|
|
453
|
+
default:
|
|
454
|
+
throw TypeError('mode is invalid');
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
/**
|
|
458
|
+
* 获取给定日期所在当年有多少天
|
|
459
|
+
* @param {Date | String | Number} date 日期
|
|
460
|
+
* @param {String} format 字符串模板
|
|
461
|
+
*
|
|
462
|
+
* @example
|
|
463
|
+
* getDayOfYear('2018-11-26') => 365
|
|
464
|
+
* getDayOfYear('12/20/2019', 'MM/dd/yyyy') => 365
|
|
465
|
+
* getDayOfYear('2020-12-20') => 366
|
|
466
|
+
*/
|
|
467
|
+
DateUtil.getDayCountOfYear = function (date, format) {
|
|
468
|
+
if (format === void 0) {
|
|
469
|
+
format = 'yyyy-MM-dd';
|
|
470
|
+
}
|
|
471
|
+
try {
|
|
472
|
+
date = DateUtil.parseDate(date);
|
|
473
|
+
} catch (_a) {
|
|
474
|
+
date = new Date();
|
|
475
|
+
}
|
|
476
|
+
return DateUtil.isLeapYear(date.getFullYear()) ? 366 : 365;
|
|
477
|
+
};
|
|
478
|
+
/**
|
|
479
|
+
* 获取给定日期在当年的第几天
|
|
480
|
+
* @param {Date | String | Number} date 日期
|
|
481
|
+
* @param {String} format 字符串模板
|
|
482
|
+
*
|
|
483
|
+
* @example
|
|
484
|
+
* getDayIndexOfYear('2017-01-20') => 20
|
|
485
|
+
* getDayIndexOfYear('2018-05-20') => 140
|
|
486
|
+
*/
|
|
487
|
+
DateUtil.getDayIndexOfYear = function (date, format) {
|
|
488
|
+
if (format === void 0) {
|
|
489
|
+
format = 'yyyy-MM-dd';
|
|
490
|
+
}
|
|
491
|
+
try {
|
|
492
|
+
date = DateUtil.parseDate(date);
|
|
493
|
+
} catch (_a) {
|
|
494
|
+
date = new Date();
|
|
495
|
+
}
|
|
496
|
+
var init = DateUtil.getDateStart(date, 'YEAR');
|
|
497
|
+
return Math.floor((date.getTime() - init.getTime()) / (24 * 60 * 60 * 1000)) + 1;
|
|
498
|
+
};
|
|
499
|
+
/**
|
|
500
|
+
* 获取给定日期在当年的第几周
|
|
501
|
+
* @param {Date | String | Number} date 日期
|
|
502
|
+
* @param {String} format 字符串模板
|
|
503
|
+
*
|
|
504
|
+
* @example
|
|
505
|
+
* getWeekIndexOfYear('2017-01-20') => 3
|
|
506
|
+
* getWeekIndexOfYear('2018-05-20') => 20
|
|
507
|
+
*/
|
|
508
|
+
DateUtil.getWeekIndexOfYear = function (date, format) {
|
|
509
|
+
if (format === void 0) {
|
|
510
|
+
format = 'yyyy-MM-dd';
|
|
511
|
+
}
|
|
512
|
+
try {
|
|
513
|
+
date = DateUtil.parseDate(date);
|
|
514
|
+
} catch (_a) {
|
|
515
|
+
date = new Date();
|
|
516
|
+
}
|
|
517
|
+
var init = DateUtil.getDateStart(date, 'YEAR');
|
|
518
|
+
return Math.floor((date.getTime() - init.getTime()) / (7 * 24 * 60 * 60 * 1000)) + 1;
|
|
519
|
+
};
|
|
520
|
+
/**
|
|
521
|
+
* 获取给定日期在当月的第几周
|
|
522
|
+
* @param {Date | String | Number} date 日期
|
|
523
|
+
* @param {String} format 字符串模板
|
|
524
|
+
*
|
|
525
|
+
* @example
|
|
526
|
+
* getWeekIndexOfMonth('2017-01-20') => 3
|
|
527
|
+
* getWeekIndexOfMonth('2018-05-20') => 3
|
|
528
|
+
*/
|
|
529
|
+
DateUtil.getWeekIndexOfMonth = function (date, format) {
|
|
530
|
+
if (format === void 0) {
|
|
531
|
+
format = 'yyyy-MM-dd';
|
|
532
|
+
}
|
|
533
|
+
try {
|
|
534
|
+
date = DateUtil.parseDate(date);
|
|
535
|
+
} catch (_a) {
|
|
536
|
+
date = new Date();
|
|
537
|
+
}
|
|
538
|
+
var init = DateUtil.getDateStart(date, 'MONTH');
|
|
539
|
+
return Math.floor((date.getTime() - init.getTime()) / (7 * 24 * 60 * 60 * 1000)) + 1;
|
|
540
|
+
};
|
|
541
|
+
/**
|
|
542
|
+
* 计算两个日期的差
|
|
543
|
+
* @param date1 日期1
|
|
544
|
+
* @param date2 日期2
|
|
545
|
+
* @param format 字符串模板
|
|
546
|
+
* @example
|
|
547
|
+
* getDateDiff('2020-12-12','2021-10-12') => {Day: 304, Hour: 0, Minute: 0, Second: 0, Millsecond: 0}
|
|
548
|
+
*/
|
|
549
|
+
DateUtil.getDateDiff = function (date1, date2, format) {
|
|
550
|
+
if (format === void 0) {
|
|
551
|
+
format = 'yyyy-MM-dd';
|
|
552
|
+
}
|
|
553
|
+
try {
|
|
554
|
+
date1 = DateUtil.parseDate(date1, format);
|
|
555
|
+
} catch (_a) {
|
|
556
|
+
date1 = new Date();
|
|
557
|
+
}
|
|
558
|
+
try {
|
|
559
|
+
date2 = DateUtil.parseDate(date2, format);
|
|
560
|
+
} catch (_b) {
|
|
561
|
+
date2 = new Date();
|
|
562
|
+
}
|
|
563
|
+
var isFirstMore = date1 > date2;
|
|
564
|
+
var timestampDiff = isFirstMore ? date1.getTime() - date2.getTime() : date2.getTime() - date1.getTime();
|
|
565
|
+
var day = Math.floor(timestampDiff / (24 * 60 * 60 * 1000));
|
|
566
|
+
var hour = Math.floor(timestampDiff % (24 * 60 * 60 * 1000) / (60 * 60 * 1000));
|
|
567
|
+
var minute = Math.floor((timestampDiff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000) / (60 * 1000));
|
|
568
|
+
var second = Math.floor((timestampDiff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - minute * 60 * 1000) / 1000);
|
|
569
|
+
var ms = timestampDiff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - minute * 60 * 1000 - second * 1000;
|
|
570
|
+
if (isFirstMore) {
|
|
571
|
+
return {
|
|
572
|
+
Day: day === 0 ? 0 : -day,
|
|
573
|
+
Hour: hour === 0 ? 0 : -hour,
|
|
574
|
+
Minute: minute === 0 ? 0 : -minute,
|
|
575
|
+
Second: second === 0 ? 0 : -second,
|
|
576
|
+
Millsecond: ms === 0 ? 0 : -ms
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
return {
|
|
580
|
+
Day: day,
|
|
581
|
+
Hour: hour,
|
|
582
|
+
Minute: minute,
|
|
583
|
+
Second: second,
|
|
584
|
+
Millsecond: ms
|
|
585
|
+
};
|
|
586
|
+
};
|
|
587
|
+
/**
|
|
588
|
+
* 判断给定日期时间是否在某日期时间之前(默认当前日期时间)
|
|
589
|
+
* @param {Date | String | Number} date1 日期时间
|
|
590
|
+
* @param {Date | String | Number} date2 日期时间(默认当前日期时间)
|
|
591
|
+
* @param {String} format 字符串模板
|
|
592
|
+
*
|
|
593
|
+
* @example
|
|
594
|
+
* isTimeBefore('2017-01-20') => true
|
|
595
|
+
* isTimeBefore('2018-05-20') => true
|
|
596
|
+
*/
|
|
597
|
+
DateUtil.isTimeBefore = function (date1, date2, format) {
|
|
598
|
+
if (date2 === void 0) {
|
|
599
|
+
date2 = new Date();
|
|
600
|
+
}
|
|
601
|
+
if (format === void 0) {
|
|
602
|
+
format = 'yyyy-MM-dd';
|
|
603
|
+
}
|
|
604
|
+
return DateUtil.timestamp(date1, format) < DateUtil.timestamp(date2, format);
|
|
605
|
+
};
|
|
606
|
+
/**
|
|
607
|
+
* 判断给定日期时间是否在某日期时间之后(默认当前日期时间)
|
|
608
|
+
* @param {Date | String | Number} date1 日期时间
|
|
609
|
+
* @param {Date | String | Number} date2 日期时间(默认当前日期时间)
|
|
610
|
+
* @param {String} format 字符串模板
|
|
611
|
+
*
|
|
612
|
+
* @example
|
|
613
|
+
* isTimeAfter('2017-01-20') => false
|
|
614
|
+
* isTimeAfter('2018-05-20') => false
|
|
615
|
+
*/
|
|
616
|
+
DateUtil.isTimeAfter = function (date1, date2, format) {
|
|
617
|
+
if (date2 === void 0) {
|
|
618
|
+
date2 = new Date();
|
|
619
|
+
}
|
|
620
|
+
if (format === void 0) {
|
|
621
|
+
format = 'yyyy-MM-dd';
|
|
622
|
+
}
|
|
623
|
+
return DateUtil.timestamp(date1, format) > DateUtil.timestamp(date2, format);
|
|
624
|
+
};
|
|
625
|
+
/**
|
|
626
|
+
* 判断给定日期时间是否在时间段内(默认当前日期时间)
|
|
627
|
+
* @param { beginTime: Date | string; endTime: Date | string; time?: Date | string } options
|
|
628
|
+
* @param {String} format 字符串模板
|
|
629
|
+
*
|
|
630
|
+
* @example
|
|
631
|
+
* const options = {beginTime:'2019-01-01',endTime:'2022-01-01',time:'2021-12-12'}
|
|
632
|
+
* isTimeExist(options) => true
|
|
633
|
+
*/
|
|
634
|
+
DateUtil.isTimeExist = function (options, format) {
|
|
635
|
+
if (format === void 0) {
|
|
636
|
+
format = 'yyyy-MM-dd';
|
|
637
|
+
}
|
|
638
|
+
var beginTime = options.beginTime,
|
|
639
|
+
endTime = options.endTime,
|
|
640
|
+
_a = options.time,
|
|
641
|
+
time = _a === void 0 ? new Date() : _a;
|
|
642
|
+
return DateUtil.timestamp(beginTime, format) < DateUtil.timestamp(time, format) && DateUtil.timestamp(time, format) < DateUtil.timestamp(endTime, format);
|
|
643
|
+
};
|
|
644
|
+
/**
|
|
645
|
+
* 补全日期区间数组
|
|
646
|
+
* @param {string} day1 '2022-01-01'
|
|
647
|
+
* @param {string} day2 '2022-01-03'
|
|
648
|
+
* @param {string} outFormat "yyyy-MM-dd" 输出的格式
|
|
649
|
+
* @returns ['2022-01-01','2022-01-02','2022-01-03']
|
|
650
|
+
*/
|
|
651
|
+
function getSectionDateList(day1, day2, outFormat) {
|
|
652
|
+
if (outFormat === void 0) {
|
|
653
|
+
outFormat = 'yyyy-MM-dd';
|
|
654
|
+
}
|
|
655
|
+
if (day1 === day2) return [day1];
|
|
656
|
+
var getDate = function getDate(str) {
|
|
657
|
+
var tempDate = new Date();
|
|
658
|
+
var list = str.split('-');
|
|
659
|
+
tempDate.setFullYear(Number(list[0]));
|
|
660
|
+
tempDate.setMonth(Number(list[1]) - 1);
|
|
661
|
+
tempDate.setDate(Number(list[2]));
|
|
662
|
+
return tempDate;
|
|
663
|
+
};
|
|
664
|
+
var date1 = getDate(day1);
|
|
665
|
+
var date2 = getDate(day2);
|
|
666
|
+
if (date1 > date2) {
|
|
667
|
+
var tempDate = date1;
|
|
668
|
+
date1 = date2;
|
|
669
|
+
date2 = tempDate;
|
|
670
|
+
}
|
|
671
|
+
date1.setDate(date1.getDate() + 1);
|
|
672
|
+
var dateArr = [];
|
|
673
|
+
var i = 0;
|
|
674
|
+
while (!(date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth() && date1.getDate() === date2.getDate())) {
|
|
675
|
+
var dayStr = date1.getDate().toString();
|
|
676
|
+
if (dayStr.length === 1) {
|
|
677
|
+
dayStr = "0".concat(dayStr);
|
|
678
|
+
}
|
|
679
|
+
dateArr[i] = "".concat(date1.getFullYear(), "-").concat(date1.getMonth() + 1, "-").concat(dayStr);
|
|
680
|
+
i += 1;
|
|
681
|
+
date1.setDate(date1.getDate() + 1);
|
|
682
|
+
}
|
|
683
|
+
dateArr.splice(0, 0, day1);
|
|
684
|
+
dateArr.push(day2);
|
|
685
|
+
return dateArr.map(function (item) {
|
|
686
|
+
return DateUtil.formatDate(DateUtil.parseDate(Number(item.split('-')[1]) < 10 ? "".concat(item.split('-')[0], "-0").concat(Number(item.split('-')[1]), "-").concat(item.split('-')[2]) : item), outFormat);
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
DateUtil.getSectionDateList = getSectionDateList;
|
|
690
|
+
/**
|
|
691
|
+
* 补全月份区间数组
|
|
692
|
+
* @param {string} day1 '2022-01'
|
|
693
|
+
* @param {string} day2 '2022-03'
|
|
694
|
+
* @param {string} outFormat "yyyy-MM" 输出的格式
|
|
695
|
+
* @returns ['2022-01','2022-02','2022-03']
|
|
696
|
+
*/
|
|
697
|
+
DateUtil.getSectionMonthList = function (day1, day2, outFormat) {
|
|
698
|
+
if (outFormat === void 0) {
|
|
699
|
+
outFormat = 'yyyy-MM';
|
|
700
|
+
}
|
|
701
|
+
var numN1 = Number(day1.split('-').join(''));
|
|
702
|
+
var numN2 = Number(day2.split('-').join(''));
|
|
703
|
+
if (numN1 === numN2) return [day1];
|
|
704
|
+
var maxMonth = numN1 - numN2 > 0 ? day1 : day2;
|
|
705
|
+
var minMonth = numN1 - numN2 > 0 ? day2 : day1;
|
|
706
|
+
var _a = __read(maxMonth.split('-'), 2),
|
|
707
|
+
year1 = _a[0],
|
|
708
|
+
month1 = _a[1];
|
|
709
|
+
var isLessTen = month1.slice(0, 1) === '0';
|
|
710
|
+
var num = Number("".concat(Number(year1) - (isLessTen ? 1 : 0)).concat(Number(month1) + (isLessTen ? 12 : 0))) - Number(minMonth.split('-').join(''));
|
|
711
|
+
var monthNum = Math.floor(num / 100) * 12 + num % 100;
|
|
712
|
+
return new Array(monthNum + 1).fill(0).map(function (_, i) {
|
|
713
|
+
return DateUtil.formatDate(DateUtil.dateAfter(minMonth, {
|
|
714
|
+
month: i + 1
|
|
715
|
+
}, outFormat), outFormat);
|
|
716
|
+
});
|
|
717
|
+
};
|
|
718
|
+
/**
|
|
719
|
+
* 补全年份区间数组
|
|
720
|
+
* @param {string} day1 '2022'
|
|
721
|
+
* @param {string} day2 '2024'
|
|
722
|
+
* @returns ['2022','2023','2024']
|
|
723
|
+
*/
|
|
724
|
+
DateUtil.getSectionYearList = function (day1, day2) {
|
|
725
|
+
var numN1 = Number(day1);
|
|
726
|
+
var numN2 = Number(day2);
|
|
727
|
+
if (numN1 === numN2) return [day1];
|
|
728
|
+
var maxYear = numN1 - numN2 > 0 ? numN1 : numN2;
|
|
729
|
+
var minYear = numN1 - numN2 > 0 ? numN2 : numN1;
|
|
730
|
+
var num = maxYear - minYear;
|
|
731
|
+
return new Array(num + 1).fill(0).map(function (_, i) {
|
|
732
|
+
return String(minYear + i);
|
|
733
|
+
});
|
|
734
|
+
};
|
|
735
|
+
/**
|
|
736
|
+
* 后端给的时间修复
|
|
737
|
+
* @param t
|
|
738
|
+
* @returns
|
|
739
|
+
*/
|
|
740
|
+
DateUtil.timeFix = function (t) {
|
|
741
|
+
var _a;
|
|
742
|
+
return (_a = t === null || t === void 0 ? void 0 : t.split('.')) === null || _a === void 0 ? void 0 : _a[0];
|
|
743
|
+
};
|
|
744
|
+
})(DateUtil || (DateUtil = {}));
|
|
745
|
+
exports.default = DateUtil;
|
|
746
|
+
var dateFormatRules = [{
|
|
747
|
+
rules: [['yyyy', 4]]
|
|
748
|
+
}, {
|
|
749
|
+
rules: [['MM', 2], ['M', 1]],
|
|
750
|
+
offset: -1
|
|
751
|
+
}, {
|
|
752
|
+
rules: [['dd', 2], ['d', 1]]
|
|
753
|
+
}, {
|
|
754
|
+
rules: [['HH', 2], ['H', 1]]
|
|
755
|
+
}, {
|
|
756
|
+
rules: [['mm', 2], ['m', 1]]
|
|
757
|
+
}, {
|
|
758
|
+
rules: [['ss', 2], ['s', 1]]
|
|
759
|
+
}, {
|
|
760
|
+
rules: [['SSS', 3], ['SS', 2], ['S', 1]]
|
|
761
|
+
}];
|
|
762
|
+
var _parseDatePart = function _parseDatePart(date) {
|
|
763
|
+
return {
|
|
764
|
+
Year: date.getFullYear(),
|
|
765
|
+
Month: date.getMonth(),
|
|
766
|
+
Date: date.getDate(),
|
|
767
|
+
Day: date.getDay(),
|
|
768
|
+
Hour: date.getHours(),
|
|
769
|
+
Minute: date.getMinutes(),
|
|
770
|
+
Second: date.getSeconds(),
|
|
771
|
+
Millsecond: date.getMilliseconds()
|
|
772
|
+
};
|
|
773
|
+
};
|