Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions packages/utils/src/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type of the format function has been changed from string to any. This could potentially lead to issues if the function is expected to always return a string. Consider ensuring that the function consistently returns a string to avoid unexpected behavior.

if (isDate(date)) {
if (typeof dateFormat === 'string') {
const o: Record<string, number | string> = {
Expand All @@ -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 ''
}

/**
Expand Down
Loading