perf[utils.js]: perf parseTime function (#2625)

This commit is contained in:
monkeycf 2019-10-08 17:54:47 +08:00 committed by 花裤衩
parent e3cbf4ad90
commit 9c723c6618
1 changed files with 4 additions and 7 deletions

View File

@ -6,7 +6,7 @@
* Parse the time to string * Parse the time to string
* @param {(Object|string|number)} time * @param {(Object|string|number)} time
* @param {string} cFormat * @param {string} cFormat
* @returns {string} * @returns {string | null}
*/ */
export function parseTime(time, cFormat) { export function parseTime(time, cFormat) {
if (arguments.length === 0) { if (arguments.length === 0) {
@ -34,14 +34,11 @@ export function parseTime(time, cFormat) {
s: date.getSeconds(), s: date.getSeconds(),
a: date.getDay() a: date.getDay()
} }
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
let value = formatObj[key] const value = formatObj[key]
// Note: getDay() returns 0 on Sunday // Note: getDay() returns 0 on Sunday
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] } if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
if (result.length > 0 && value < 10) { return value.toString().padStart(2, '0')
value = '0' + value
}
return value || 0
}) })
return time_str return time_str
} }