perf[utils.js]: perf parseTime function

This commit is contained in:
花裤衩 2019-10-22 20:29:20 +08:00
parent 0e8fa65568
commit f32b49e662
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
} }