tools-min-ns 1.14.0 → 1.14.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 CHANGED
@@ -5,6 +5,11 @@ cnpm i tools-min-ns --save
5
5
  ```
6
6
 
7
7
  # updates
8
+ ## v1.14.2
9
+ > DateUtil 日期格式化兼容优化
10
+ ## v1.14.1
11
+ > DateUtil 日期格式化兼容YYYY-MM-DD
12
+
8
13
  ## v1.14.0
9
14
  > ArrayUtil 新增三个分组方法
10
15
  ## v1.13.0
@@ -37,14 +37,15 @@ declare namespace DateUtil {
37
37
  const parseDate: (date: Date | string | number, format?: string) => Date;
38
38
  /**
39
39
  * 将日期格式化成指定格式的字符串
40
+ * 兼容YYYY-MM-DD/yyyy-MM-dd
40
41
  * @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
41
- * @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
42
+ * @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
42
43
  * @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
43
44
  * @example
44
45
  * formatDate(); => 2016-09-02 13:17:13
45
46
  * formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
46
47
  * formatDate(new Date(), 'yyyy-MM-dd'); => 2016-09-02
47
- * formatDate(new Date(), 'yyyy-MM-dd 第q季度 www HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
48
+ * formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
48
49
  * formatDate(1472793615764); => 2016-09-02 13:20:15
49
50
  */
50
51
  const formatDate: (date?: Date | number | string, format?: string) => string;
@@ -65,7 +65,7 @@ var DateUtil;
65
65
  dateFormatRules.forEach(function (item) {
66
66
  for (var index = 0, rules = item.rules, len = rules.length; index < len; index++) {
67
67
  rule = rules[index];
68
- sIndex = format.indexOf("".concat(rule[0]));
68
+ sIndex = format.replaceAll('D', 'd').replaceAll('Y', 'y').indexOf("".concat(rule[0]));
69
69
  if (sIndex !== -1) {
70
70
  dates.push(parseFloat(dateStr.substring(sIndex, sIndex + Number(rule[1]))) + (item.offset || 0));
71
71
  // 如果匹配到规则中的第一条,则退出
@@ -80,14 +80,15 @@ var DateUtil;
80
80
  };
81
81
  /**
82
82
  * 将日期格式化成指定格式的字符串
83
+ * 兼容YYYY-MM-DD/yyyy-MM-dd
83
84
  * @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
84
- * @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
85
+ * @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
85
86
  * @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
86
87
  * @example
87
88
  * formatDate(); => 2016-09-02 13:17:13
88
89
  * formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
89
90
  * formatDate(new Date(), 'yyyy-MM-dd'); => 2016-09-02
90
- * formatDate(new Date(), 'yyyy-MM-dd 第q季度 www HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
91
+ * formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
91
92
  * formatDate(1472793615764); => 2016-09-02 13:20:15
92
93
  */
93
94
  DateUtil.formatDate = function (date, format) {
@@ -107,36 +108,22 @@ var DateUtil;
107
108
  } else {
108
109
  return 'invalid date';
109
110
  }
110
- date = date;
111
- var obj = {
112
- y: date.getFullYear(),
113
- M: date.getMonth() + 1,
114
- d: date.getDate(),
115
- q: Math.floor((date.getMonth() + 3) / 3),
116
- w: date.getDay(),
117
- H: date.getHours(),
118
- h: date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
119
- m: date.getMinutes(),
120
- s: date.getSeconds(),
121
- S: date.getMilliseconds() // 毫秒
122
- };
123
- var week = ['天', '一', '二', '三', '四', '五', '六'];
124
- var _loop_1 = function _loop_1(i) {
125
- format = format.replace(new RegExp(i + '+', 'g'), function (m) {
126
- var val = "".concat(obj[i]);
127
- if (i === 'w') {
128
- return (m.length > 2 ? '星期' : '周') + week[Number(val)];
129
- }
130
- for (var j = 0, len = val.length; j < m.length - len; j++) {
131
- val = '0' + val;
132
- }
133
- return m.length === 1 ? val : val.substring(val.length - m.length);
134
- });
111
+ var d = date;
112
+ var formatChars = {
113
+ YYYY: d.getFullYear().toString(),
114
+ yyyy: d.getFullYear().toString(),
115
+ MM: ('0' + (d.getMonth() + 1)).slice(-2),
116
+ DD: ('0' + d.getDate()).slice(-2),
117
+ dd: ('0' + d.getDate()).slice(-2),
118
+ HH: ('0' + d.getHours()).slice(-2),
119
+ mm: ('0' + d.getMinutes()).slice(-2),
120
+ ss: ('0' + d.getSeconds()).slice(-2)
135
121
  };
136
- for (var i in obj) {
137
- _loop_1(i);
122
+ var formattedDate = format;
123
+ for (var char in formatChars) {
124
+ formattedDate = formattedDate.replace(char, formatChars[char]);
138
125
  }
139
- return format;
126
+ return formattedDate;
140
127
  };
141
128
  /**
142
129
  * 将一个日期格式化成友好格式,比如,1分钟以内的返回“刚刚”,
@@ -37,14 +37,15 @@ declare namespace DateUtil {
37
37
  const parseDate: (date: Date | string | number, format?: string) => Date;
38
38
  /**
39
39
  * 将日期格式化成指定格式的字符串
40
+ * 兼容YYYY-MM-DD/yyyy-MM-dd
40
41
  * @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
41
- * @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
42
+ * @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
42
43
  * @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
43
44
  * @example
44
45
  * formatDate(); => 2016-09-02 13:17:13
45
46
  * formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
46
47
  * formatDate(new Date(), 'yyyy-MM-dd'); => 2016-09-02
47
- * formatDate(new Date(), 'yyyy-MM-dd 第q季度 www HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
48
+ * formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
48
49
  * formatDate(1472793615764); => 2016-09-02 13:20:15
49
50
  */
50
51
  const formatDate: (date?: Date | number | string, format?: string) => string;
@@ -75,7 +75,7 @@ var DateUtil;
75
75
  dateFormatRules.forEach(function (item) {
76
76
  for (var index = 0, rules = item.rules, len = rules.length; index < len; index++) {
77
77
  rule = rules[index];
78
- sIndex = format.indexOf("".concat(rule[0]));
78
+ sIndex = format.replaceAll('D', 'd').replaceAll('Y', 'y').indexOf("".concat(rule[0]));
79
79
  if (sIndex !== -1) {
80
80
  dates.push(parseFloat(dateStr.substring(sIndex, sIndex + Number(rule[1]))) + (item.offset || 0));
81
81
  // 如果匹配到规则中的第一条,则退出
@@ -90,14 +90,15 @@ var DateUtil;
90
90
  };
91
91
  /**
92
92
  * 将日期格式化成指定格式的字符串
93
+ * 兼容YYYY-MM-DD/yyyy-MM-dd
93
94
  * @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
+ * @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
95
96
  * @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
96
97
  * @example
97
98
  * formatDate(); => 2016-09-02 13:17:13
98
99
  * formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
99
100
  * 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(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
101
102
  * formatDate(1472793615764); => 2016-09-02 13:20:15
102
103
  */
103
104
  DateUtil.formatDate = function (date, format) {
@@ -117,36 +118,22 @@ var DateUtil;
117
118
  } else {
118
119
  return 'invalid date';
119
120
  }
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
- });
121
+ var d = date;
122
+ var formatChars = {
123
+ YYYY: d.getFullYear().toString(),
124
+ yyyy: d.getFullYear().toString(),
125
+ MM: ('0' + (d.getMonth() + 1)).slice(-2),
126
+ DD: ('0' + d.getDate()).slice(-2),
127
+ dd: ('0' + d.getDate()).slice(-2),
128
+ HH: ('0' + d.getHours()).slice(-2),
129
+ mm: ('0' + d.getMinutes()).slice(-2),
130
+ ss: ('0' + d.getSeconds()).slice(-2)
145
131
  };
146
- for (var i in obj) {
147
- _loop_1(i);
132
+ var formattedDate = format;
133
+ for (var char in formatChars) {
134
+ formattedDate = formattedDate.replace(char, formatChars[char]);
148
135
  }
149
- return format;
136
+ return formattedDate;
150
137
  };
151
138
  /**
152
139
  * 将一个日期格式化成友好格式,比如,1分钟以内的返回“刚刚”,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tools-min-ns",
3
3
  "description": "工具包适用于前端以及node",
4
- "version": "1.14.0",
4
+ "version": "1.14.2",
5
5
  "main": "lib/index.js",
6
6
  "license": "MIT",
7
7
  "author": "nanshen",