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

Add missing padding to 'YYYY' format #2231

Merged
merged 2 commits into from
Feb 13, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class Dayjs {

const matches = {
YY: String(this.$y).slice(-2),
YYYY: this.$y,
YYYY: Utils.s(this.$y, 4, '0'),
M: $M + 1,
MM: Utils.s($M + 1, 2, '0'),
MMM: getShort(locale.monthsShort, $M, months, 3),
Expand Down
12 changes: 12 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,15 @@ it('As JSON -> toJSON', () => {
it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
expect(dayjs().toISOString()).toBe(moment().toISOString())
})

it('Year 1 formatted with YYYY should pad with zeroes', () => {
const date = new Date(1, 0, 1)
date.setUTCFullYear(1) // Required because 0-99 are parsed as 19xx in JS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#year
expect(dayjs(date).format('YYYY')).toBe('0001')
})

it('Year 1 formatting matches moment format', () => {
const date = new Date(1, 0, 1)
date.setUTCFullYear(1)
expect(dayjs(date).format('YYYY')).toBe(moment(date).format('YYYY'))
})