|
|
@@ -274,6 +274,43 @@ function MagicJS(scriptName = "MagicJS", logLevel = "INFO") {
|
|
|
}
|
|
|
return newObj;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * formatDate y:年 M:月 d:日 q:季 H:时 m:分 s:秒 S:毫秒
|
|
|
+ */
|
|
|
+ formatDate(date, format) {
|
|
|
+ let o = {
|
|
|
+ 'M+': date.getMonth() + 1,
|
|
|
+ 'd+': date.getDate(),
|
|
|
+ 'H+': date.getHours(),
|
|
|
+ 'm+': date.getMinutes(),
|
|
|
+ 's+': date.getSeconds(),
|
|
|
+ 'q+': Math.floor((date.getMonth() + 3) / 3),
|
|
|
+ 'S': date.getMilliseconds()
|
|
|
+ }
|
|
|
+ if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
|
|
|
+ for (let k in o)
|
|
|
+ if (new RegExp('(' + k + ')').test(format))
|
|
|
+ format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
|
|
|
+ return format
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * parseDate 字符串格式,默认'yyyy-MM-dd',支持如下:y、M、d、H、m、s、S,不支持w和q
|
|
|
+ */
|
|
|
+ parseDate(str, format) {
|
|
|
+ format = format || 'yyyy-MM-dd';
|
|
|
+ let obj = { y: 0, M: 1, d: 0, H: 0, h: 0, m: 0, s: 0, S: 0 };
|
|
|
+ format.replace(/([^yMdHmsS]*?)(([yMdHmsS])\3*)([^yMdHmsS]*?)/g, function (m, $1, $2, $3, $4, idx, old) {
|
|
|
+ str = str.replace(new RegExp($1 + '(\\d{' + $2.length + '})' + $4), function (_m, _$1) {
|
|
|
+ obj[$3] = parseInt(_$1);
|
|
|
+ return '';
|
|
|
+ });
|
|
|
+ return '';
|
|
|
+ });
|
|
|
+ obj.M--; // 月份是从0开始的,所以要减去1
|
|
|
+ let date = new Date(obj.y, obj.M, obj.d, obj.H, obj.m, obj.s);
|
|
|
+ if (obj.S !== 0) date.setMilliseconds(obj.S); // 如果设置了毫秒
|
|
|
+ return date;
|
|
|
+ }
|
|
|
costTime() {
|
|
|
let info = `${this.scriptName}执行完毕!`
|
|
|
// if (this.isNode && this.isExecComm) {
|