Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useDateFormat): support MMM and MMMM formatter #2234

Merged
merged 1 commit into from Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/shared/useDateFormat/index.md
Expand Up @@ -9,11 +9,13 @@ Get the formatted date according to the string of tokens passed in, inspired by
**List of all available formats (HH:mm:ss by default):**

| Format | Output | Description |
| ------ | ---------------- | ------------------------------------- |
|--------| ---------------- |---------------------------------------|
| `YY` | 18 | Two-digit year |
| `YYYY` | 2018 | Four-digit year |
| `M` | 1-12 | The month, beginning at 1 |
| `MM` | 01-12 | The month, 2-digits |
| `MMM` | Jan-Dec | The abbreviated month name |
| `MMMM` | January-December | The full month name |
| `D` | 1-31 | The day of the month |
| `DD` | 01-31 | The day of the month, 2-digits |
| `H` | 0-23 | The hour |
Expand Down
6 changes: 6 additions & 0 deletions packages/shared/useDateFormat/index.test.ts
Expand Up @@ -37,4 +37,10 @@ describe('useDateFormat', () => {
it('should work with YYYY/MM/DD dddd', () => {
expect(useDateFormat(new Date('2022-01-01 15:05:05'), 'YYYY/MM/DD dddd', { locales: 'en-US' }).value).toBe('2022/01/01 Saturday')
})
it('should work with MMM DD YYYY', () => {
expect(useDateFormat(new Date('2022-01-01 15:05:05'), 'MMM DD YYYY', { locales: 'en-US' }).value).toBe('Jan 01 2022')
})
it('should work with MMMM DD YYYY', () => {
expect(useDateFormat(new Date('2022-01-01 15:05:05'), 'MMMM DD YYYY', { locales: 'en-US' }).value).toBe('January 01 2022')
})
})
2 changes: 2 additions & 0 deletions packages/shared/useDateFormat/index.ts
Expand Up @@ -30,6 +30,8 @@ export const formatDate = (date: Date, formatStr: string, locales?: Intl.Locales
YYYY: () => years,
M: () => month + 1,
MM: () => `${month + 1}`.padStart(2, '0'),
MMM: () => date.toLocaleDateString(locales, { month: 'short' }),
MMMM: () => date.toLocaleDateString(locales, { month: 'long' }),
D: () => String(days),
DD: () => `${days}`.padStart(2, '0'),
H: () => String(hours),
Expand Down