Skip to content

Commit 00c223b

Browse files
authored
fix: .format add padding to 'YYYY' (iamkun#2231)
1 parent b87aa0e commit 00c223b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class Dayjs {
280280

281281
const matches = {
282282
YY: String(this.$y).slice(-2),
283-
YYYY: this.$y,
283+
YYYY: Utils.s(this.$y, 4, '0'),
284284
M: $M + 1,
285285
MM: Utils.s($M + 1, 2, '0'),
286286
MMM: getShort(locale.monthsShort, $M, months, 3),

test/display.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,15 @@ it('As JSON -> toJSON', () => {
260260
it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
261261
expect(dayjs().toISOString()).toBe(moment().toISOString())
262262
})
263+
264+
it('Year 1 formatted with YYYY should pad with zeroes', () => {
265+
const date = new Date(1, 0, 1)
266+
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
267+
expect(dayjs(date).format('YYYY')).toBe('0001')
268+
})
269+
270+
it('Year 1 formatting matches moment format', () => {
271+
const date = new Date(1, 0, 1)
272+
date.setUTCFullYear(1)
273+
expect(dayjs(date).format('YYYY')).toBe(moment(date).format('YYYY'))
274+
})

0 commit comments

Comments
 (0)