Skip to content

Commit ffdd027

Browse files
authored
fix(utils): modify the date formatting function, remove redundant parameters and optimize the logic (opentiny#3169)
1 parent af2a482 commit ffdd027

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

packages/utils/src/date/index.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ interface MaxDateValues {
415415
* @param afterFormat - 转换后的格式(仅当date为字符串且有3个参数时有效)
416416
* @returns 格式化后的日期字符串
417417
*/
418-
export const format = function (date: Date | string, dateFormat = 'yyyy/MM/dd hh:mm:ss', afterFormat?: string): string {
418+
export const format = function (date: Date | string, dateFormat = 'yyyy/MM/dd hh:mm:ss'): any {
419419
if (isDate(date)) {
420420
if (typeof dateFormat === 'string') {
421421
const o: Record<string, number | string> = {
@@ -434,28 +434,24 @@ export const format = function (date: Date | string, dateFormat = 'yyyy/MM/dd hh
434434
const m = dateFormat.match(dateFormatRegs[k])
435435

436436
if (k && m && m.length) {
437-
const replacement = k === 'Z{1,1}' ? (o[k] as string) : fillChar(o[k].toString(), m[0].length, false, '0')
438-
dateFormat = dateFormat.replace(m[0], replacement)
437+
dateFormat = dateFormat.replace(m[0], k === 'Z{1,1}' ? o[k] : fillChar(o[k].toString(), m[0].length))
439438
}
440439
})
441440

442441
return dateFormat
443442
}
444443
} else if (typeof date === 'string' && arguments.length >= 2) {
445-
let actualDateFormat = dateFormat
446-
let actualAfterFormat = dateFormat
444+
let afterFormat = dateFormat
447445

448446
if (arguments.length === 2) {
449-
actualDateFormat = ''
447+
dateFormat = undefined
450448
} else {
451-
actualAfterFormat = arguments[2] as string
449+
afterFormat = arguments[2]
452450
}
453451

454-
const dateValue = toDate(date, actualDateFormat)
455-
return dateValue ? format(dateValue, actualAfterFormat) : ''
452+
const dateValue = toDate(date, dateFormat)
453+
return dateValue ? format(dateValue, afterFormat) : ''
456454
}
457-
458-
return ''
459455
}
460456

461457
/**

0 commit comments

Comments
 (0)