Skip to content

Commit

Permalink
fix: .format add padding to 'YYYY' (#2231)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharparam committed Feb 13, 2023
1 parent b87aa0e commit 00c223b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
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
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'))
})

0 comments on commit 00c223b

Please sign in to comment.