tools-min-ns 1.14.0 → 1.14.1

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,9 @@ cnpm i tools-min-ns --save
5
5
  ```
6
6
 
7
7
  # updates
8
+ ## v1.14.1
9
+ > DateUtil 日期格式化兼容YYYY-MM-DD
10
+
8
11
  ## v1.14.0
9
12
  > ArrayUtil 新增三个分组方法
10
13
  ## v1.13.0
@@ -38,13 +38,13 @@ declare namespace DateUtil {
38
38
  /**
39
39
  * 将日期格式化成指定格式的字符串
40
40
  * @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
41
- * @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
41
+ * @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
42
42
  * @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
43
43
  * @example
44
44
  * formatDate(); => 2016-09-02 13:17:13
45
45
  * formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
46
46
  * 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
47
+ * formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
48
48
  * formatDate(1472793615764); => 2016-09-02 13:20:15
49
49
  */
50
50
  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
  // 如果匹配到规则中的第一条,则退出
@@ -81,13 +81,13 @@ var DateUtil;
81
81
  /**
82
82
  * 将日期格式化成指定格式的字符串
83
83
  * @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
84
- * @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
84
+ * @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
85
85
  * @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
86
86
  * @example
87
87
  * formatDate(); => 2016-09-02 13:17:13
88
88
  * formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
89
89
  * 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
90
+ * formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
91
91
  * formatDate(1472793615764); => 2016-09-02 13:20:15
92
92
  */
93
93
  DateUtil.formatDate = function (date, format) {
@@ -108,35 +108,38 @@ var DateUtil;
108
108
  return 'invalid date';
109
109
  }
110
110
  date = date;
111
- var obj = {
111
+ var map = {
112
112
  y: date.getFullYear(),
113
+ Y: date.getFullYear(),
113
114
  M: date.getMonth() + 1,
114
115
  d: date.getDate(),
116
+ D: date.getDate(),
115
117
  q: Math.floor((date.getMonth() + 3) / 3),
116
- w: date.getDay(),
118
+ w: ['日', '一', '二', '三', '四', '五', '六'][date.getDay()],
117
119
  H: date.getHours(),
118
120
  h: date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
119
121
  m: date.getMinutes(),
120
- s: date.getSeconds(),
121
- S: date.getMilliseconds() // 毫秒
122
+ S: date.getMilliseconds(),
123
+ s: date.getSeconds()
122
124
  };
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
- });
125
+ var formatNumber = function formatNumber(n) {
126
+ return n.toString().padStart(2, '0');
135
127
  };
136
- for (var i in obj) {
137
- _loop_1(i);
138
- }
139
- return format;
128
+ var formatMilliseconds = function formatMilliseconds(n) {
129
+ return n.toString().padStart(3, '0');
130
+ };
131
+ return format.replace(/(y+|Y+|M+|d+|D+|q+|w+|H+|h+|m+|s+|S+)/g, function (match) {
132
+ var char = match[0];
133
+ var result = map[char];
134
+ if (char.toLowerCase() === 'y') {
135
+ result = result.toString().substr(4 - match.length);
136
+ } else if (char === 'S') {
137
+ result = formatMilliseconds(result);
138
+ } else if (['M', 'd', 'D', 'H', 'h', 'm', 's'].includes(char)) {
139
+ result = formatNumber(result);
140
+ }
141
+ return result;
142
+ });
140
143
  };
141
144
  /**
142
145
  * 将一个日期格式化成友好格式,比如,1分钟以内的返回“刚刚”,
@@ -38,13 +38,13 @@ declare namespace DateUtil {
38
38
  /**
39
39
  * 将日期格式化成指定格式的字符串
40
40
  * @param {Date | Number | String} date 要格式化的日期,不传时默认当前时间,也可以是一个时间戳/字符串
41
- * @param {String} format 目标字符串格式,支持的字符有:y,M,d,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
41
+ * @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
42
42
  * @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
43
43
  * @example
44
44
  * formatDate(); => 2016-09-02 13:17:13
45
45
  * formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
46
46
  * 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
47
+ * formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
48
48
  * formatDate(1472793615764); => 2016-09-02 13:20:15
49
49
  */
50
50
  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
  // 如果匹配到规则中的第一条,则退出
@@ -91,13 +91,13 @@ var DateUtil;
91
91
  /**
92
92
  * 将日期格式化成指定格式的字符串
93
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
94
+ * @param {String} format 目标字符串格式,支持的字符有:y,Y,M,d,D,q,w,H,h,m,S,默认:yyyy-MM-dd HH:mm:ss
95
95
  * @returns {String} 返回格式化后的日期字符串,日期不合法时返回字符串 'invalid date'
96
96
  * @example
97
97
  * formatDate(); => 2016-09-02 13:17:13
98
98
  * formatDate('2016-09-02','yyyy年MM月dd日'); => 2016年09月02日
99
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
100
+ * formatDate(new Date(), 'yyyy-MM-dd 第q季度 星期w HH:mm:ss:SSS'); => 2016-09-02 第3季度 星期五 13:19:15:792
101
101
  * formatDate(1472793615764); => 2016-09-02 13:20:15
102
102
  */
103
103
  DateUtil.formatDate = function (date, format) {
@@ -118,35 +118,38 @@ var DateUtil;
118
118
  return 'invalid date';
119
119
  }
120
120
  date = date;
121
- var obj = {
121
+ var map = {
122
122
  y: date.getFullYear(),
123
+ Y: date.getFullYear(),
123
124
  M: date.getMonth() + 1,
124
125
  d: date.getDate(),
126
+ D: date.getDate(),
125
127
  q: Math.floor((date.getMonth() + 3) / 3),
126
- w: date.getDay(),
128
+ w: ['日', '一', '二', '三', '四', '五', '六'][date.getDay()],
127
129
  H: date.getHours(),
128
130
  h: date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
129
131
  m: date.getMinutes(),
130
- s: date.getSeconds(),
131
- S: date.getMilliseconds() // 毫秒
132
+ S: date.getMilliseconds(),
133
+ s: date.getSeconds()
132
134
  };
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
- });
135
+ var formatNumber = function formatNumber(n) {
136
+ return n.toString().padStart(2, '0');
145
137
  };
146
- for (var i in obj) {
147
- _loop_1(i);
148
- }
149
- return format;
138
+ var formatMilliseconds = function formatMilliseconds(n) {
139
+ return n.toString().padStart(3, '0');
140
+ };
141
+ return format.replace(/(y+|Y+|M+|d+|D+|q+|w+|H+|h+|m+|s+|S+)/g, function (match) {
142
+ var char = match[0];
143
+ var result = map[char];
144
+ if (char.toLowerCase() === 'y') {
145
+ result = result.toString().substr(4 - match.length);
146
+ } else if (char === 'S') {
147
+ result = formatMilliseconds(result);
148
+ } else if (['M', 'd', 'D', 'H', 'h', 'm', 's'].includes(char)) {
149
+ result = formatNumber(result);
150
+ }
151
+ return result;
152
+ });
150
153
  };
151
154
  /**
152
155
  * 将一个日期格式化成友好格式,比如,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.1",
5
5
  "main": "lib/index.js",
6
6
  "license": "MIT",
7
7
  "author": "nanshen",