Skip to content

Commit a26b3df

Browse files
monkeycfTrong. Pham Van - CMC Global DU1.19
authored and
Trong. Pham Van - CMC Global DU1.19
committed
perf[utils.js]: perf parseTime function (PanJiaChen#2625)
1 parent ab55984 commit a26b3df

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/utils/index.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Parse the time to string
77
* @param {(Object|string|number)} time
88
* @param {string} cFormat
9-
* @returns {string}
9+
* @returns {string | null}
1010
*/
1111
export function parseTime(time, cFormat) {
1212
if (arguments.length === 0) {
@@ -34,14 +34,11 @@ export function parseTime(time, cFormat) {
3434
s: date.getSeconds(),
3535
a: date.getDay()
3636
}
37-
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
38-
let value = formatObj[key]
37+
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
38+
const value = formatObj[key]
3939
// Note: getDay() returns 0 on Sunday
4040
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
41-
if (result.length > 0 && value < 10) {
42-
value = '0' + value
43-
}
44-
return value || 0
41+
return value.toString().padStart(2, '0')
4542
})
4643
return time_str
4744
}

0 commit comments

Comments
 (0)