Skip to content

Commit

Permalink
fix(useDateFormat): handle zero properly (#3272)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
JocelynFloresz and antfu committed Jul 30, 2023
1 parent b5e52f5 commit d642866
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/shared/useDateFormat/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@ describe('useDateFormat', () => {
expect(useDateFormat(new Date(dateStr), formatStr, { customMeridiem }).value).toBe(expected)
})
})

it('formatDate', () => {
expect(formatDate(new Date('Sun Jul 30 2023 21:15:42 GMT+0800'), 'd'))
.toMatchInlineSnapshot('"0"')
})
})
2 changes: 1 addition & 1 deletion packages/shared/useDateFormat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function formatDate(date: Date, formatStr: string, options: UseDateFormat
a: () => meridiem(hours, minutes, true),
aa: () => meridiem(hours, minutes, true, true),
}
return formatStr.replace(REGEX_FORMAT, (match, $1) => $1 || matches[match]?.() || match)
return formatStr.replace(REGEX_FORMAT, (match, $1) => $1 ?? matches[match]?.() ?? match)
}

export function normalizeDate(date: DateLike) {
Expand Down

0 comments on commit d642866

Please sign in to comment.