@tmsfe/tms-core 0.0.55 → 0.0.56
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/package.json +1 -1
- package/src/index.js +2 -0
- package/src/timeUtils.js +37 -20
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
formatTime,
|
|
29
29
|
formatTimeStr,
|
|
30
30
|
formatTimeWithDetails,
|
|
31
|
+
formatDateTime,
|
|
31
32
|
dateToString,
|
|
32
33
|
} from './timeUtils';
|
|
33
34
|
import {
|
|
@@ -159,6 +160,7 @@ const api = {
|
|
|
159
160
|
formatTime,
|
|
160
161
|
formatTimeStr,
|
|
161
162
|
formatTimeWithDetails,
|
|
163
|
+
formatDateTime,
|
|
162
164
|
dateToString,
|
|
163
165
|
|
|
164
166
|
/* IPX方法 */
|
package/src/timeUtils.js
CHANGED
|
@@ -110,9 +110,41 @@ const formatTimeStr = (str = '', dateSeprator = '.', keepSeconds = false) => {
|
|
|
110
110
|
return s;
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
+
/**
|
|
114
|
+
* 格式化时间对象
|
|
115
|
+
* @param {Date|Number} date Date对象
|
|
116
|
+
* @param {String} fmt 目标格式,如:yyyy年MM月dd日,MM/dd/yyyy,yyyyMMdd,yyyy-MM-dd hh:mm:ss等
|
|
117
|
+
* @returns {String} 格式化结果;异常情况下返回空串
|
|
118
|
+
*/
|
|
119
|
+
const formatDateTime = (date, fmt = 'yyyy-MM-dd hh:mm:ss') => {
|
|
120
|
+
const dateObj = date instanceof Date ? date : new Date(date);
|
|
121
|
+
if (isNaN(dateObj.getTime())) return '';
|
|
122
|
+
|
|
123
|
+
const obj = {
|
|
124
|
+
'M+': dateObj.getMonth() + 1, // 月份
|
|
125
|
+
'd+': dateObj.getDate(), // 日
|
|
126
|
+
'h+': dateObj.getHours(), // 小时
|
|
127
|
+
'm+': dateObj.getMinutes(), // 分
|
|
128
|
+
's+': dateObj.getSeconds(), // 秒
|
|
129
|
+
S: dateObj.getMilliseconds(), // 毫秒
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
let dateStr = fmt || 'yyyy-MM-dd hh:mm:ss';
|
|
133
|
+
if (/(y+)/.test(dateStr)) {
|
|
134
|
+
dateStr = dateStr.replace(RegExp.$1, (`${dateObj.getFullYear()}`).substr(4 - RegExp.$1.length));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
138
|
+
if (new RegExp(`(${key})`).test(dateStr)) {
|
|
139
|
+
dateStr = dateStr.replace(RegExp.$1, (RegExp.$1.length === 1) ? (value) : (`${value}`.padStart(2, '0')));
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return dateStr;
|
|
144
|
+
};
|
|
113
145
|
|
|
114
146
|
/**
|
|
115
|
-
* @description 格式化时间戳为 yyyy-
|
|
147
|
+
* @description 格式化时间戳为 yyyy-MM-dd, yyyy-MM-dd hh:mm, yyyy-MM-dd hh:mm:ss
|
|
116
148
|
* @param {Date} date 日期
|
|
117
149
|
* @param {Boolean} withTime 是否带时间
|
|
118
150
|
* @param {Boolean} withSeconds 是否带秒数
|
|
@@ -120,28 +152,13 @@ const formatTimeStr = (str = '', dateSeprator = '.', keepSeconds = false) => {
|
|
|
120
152
|
* @returns {String} 格式化后的字符串,如 2021-03-18 或者 2021-03-18 10:11
|
|
121
153
|
*/
|
|
122
154
|
const dateToString = (date, withTime = false, withSeconds = false, join = '-') => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const month = DATE.getMonth() + 1;
|
|
127
|
-
const day = DATE.getDate();
|
|
128
|
-
const time = DATE.toTimeString().slice(0, 8);
|
|
129
|
-
let dateStr = year + join + month + join + day;
|
|
130
|
-
|
|
131
|
-
if (!withTime) {
|
|
132
|
-
return dateStr.replace(/\b\d\b/g, '0$&');
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
dateStr = `${dateStr} ${time}`.replace(/\b\d\b/g, '0$&');
|
|
136
|
-
|
|
137
|
-
if (!withSeconds) {
|
|
138
|
-
dateStr = dateStr.slice(0, -3);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return dateStr;
|
|
155
|
+
let fmt = `yyyy${join}MM${join}dd`;
|
|
156
|
+
if (withTime) fmt += withSeconds ? 'hh:mm:ss' : 'hh:mm';
|
|
157
|
+
return formatDateTime(date, fmt);
|
|
142
158
|
};
|
|
143
159
|
|
|
144
160
|
export {
|
|
161
|
+
formatDateTime,
|
|
145
162
|
formatTime,
|
|
146
163
|
formatTimeStr,
|
|
147
164
|
formatTimeWithDetails,
|