fix(utils): modify the date formatting function, remove redundant parameters and optimize the logic (#3169)

This commit is contained in:
ajaxzheng 2025-03-21 16:03:31 +08:00 committed by GitHub
parent af2a4822a9
commit ffdd027ae8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 11 deletions

View File

@ -415,7 +415,7 @@ interface MaxDateValues {
* @param afterFormat - date为字符串且有3个参数时有效
* @returns
*/
export const format = function (date: Date | string, dateFormat = 'yyyy/MM/dd hh:mm:ss', afterFormat?: string): string {
export const format = function (date: Date | string, dateFormat = 'yyyy/MM/dd hh:mm:ss'): any {
if (isDate(date)) {
if (typeof dateFormat === 'string') {
const o: Record<string, number | string> = {
@ -434,28 +434,24 @@ export const format = function (date: Date | string, dateFormat = 'yyyy/MM/dd hh
const m = dateFormat.match(dateFormatRegs[k])
if (k && m && m.length) {
const replacement = k === 'Z{1,1}' ? (o[k] as string) : fillChar(o[k].toString(), m[0].length, false, '0')
dateFormat = dateFormat.replace(m[0], replacement)
dateFormat = dateFormat.replace(m[0], k === 'Z{1,1}' ? o[k] : fillChar(o[k].toString(), m[0].length))
}
})
return dateFormat
}
} else if (typeof date === 'string' && arguments.length >= 2) {
let actualDateFormat = dateFormat
let actualAfterFormat = dateFormat
let afterFormat = dateFormat
if (arguments.length === 2) {
actualDateFormat = ''
dateFormat = undefined
} else {
actualAfterFormat = arguments[2] as string
afterFormat = arguments[2]
}
const dateValue = toDate(date, actualDateFormat)
return dateValue ? format(dateValue, actualAfterFormat) : ''
const dateValue = toDate(date, dateFormat)
return dateValue ? format(dateValue, afterFormat) : ''
}
return ''
}
/**