Skip to content

Commit

Permalink
fix: .format add padding to 'YYYY' (iamkun#2231)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharparam authored and Сеничев Александр Вадимович committed Mar 3, 2023
1 parent b87aa0e commit 90c3ac9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
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
14 changes: 12 additions & 2 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const matchWord = /\d*[^-_:/,()\s\d]+/ // Word

let locale = {}

const getMonth = (matchIndex) => {
if (matchIndex !== 12 && matchIndex % 12 === 0) {
return 12
}

return matchIndex % 12 || matchIndex
}

let parseTwoDigitYear = function (input) {
input = +input
return input + (input > 68 ? 1900 : 2000)
Expand Down Expand Up @@ -104,15 +112,17 @@ const expressions = {
if (matchIndex < 1) {
throw new Error()
}
this.month = (matchIndex % 12) || matchIndex

this.month = getMonth(matchIndex)
}],
MMMM: [matchWord, function (input) {
const months = getLocalePart('months')
const matchIndex = months.indexOf(input) + 1
if (matchIndex < 1) {
throw new Error()
}
this.month = (matchIndex % 12) || matchIndex

this.month = getMonth(matchIndex)
}],
Y: [matchSigned, addInput('year')],
YY: [match2, function (input) {
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'))
})
10 changes: 10 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ it('correctly parse ordinal', () => {
.toBe(momentCN.locale())
})

describe('month doesn\'t lead to the next year', () => {
it('MMMM', () => {
const input = '03 декабря'
const format1 = 'D MMMM'
const format2 = 'D MMMMF'
expect(dayjs(input, format1, 'ru').valueOf()).toBe(moment(input, format1, 'ru').valueOf())
expect(dayjs(input, format2, 'ru').valueOf()).toBe(moment(input, format2, 'ru').valueOf())
})
})

describe('month function locale', () => {
it('MMMM', () => {
const input = '08 мая 2020'
Expand Down

0 comments on commit 90c3ac9

Please sign in to comment.