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

fix: add test cases for MM-DD format date parsing #2852

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [1.11.13](https://github.com/iamkun/dayjs/compare/v1.11.12...v1.11.13) (2024-08-20)


### Bug Fixes

* customParseFormat supports Q quter / w ww weekOfYear ([#2705](https://github.com/iamkun/dayjs/issues/2705)) ([8ca74f1](https://github.com/iamkun/dayjs/commit/8ca74f178eff4bb4eb686676cf35fe7edb815536))

## [1.11.12](https://github.com/iamkun/dayjs/compare/v1.11.11...v1.11.12) (2024-07-18)


Expand Down
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ const parseDate = (cfg) => {
return new Date(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
}

/**
* Handle 'MM-DD' format (e.g. '10-10') which is not caught by REGEX_PARSE
* @see https://github.com/iamkun/dayjs/issues/2844
*/
if (/^\d{1,2}[-/.]\d{1,2}$/.test(date)) {
const parts = date.split(/[-/.]/)
const month = parseInt(parts[0], 10) - 1
const day = parseInt(parts[1], 10)

// Check if month and day are valid
if (month >= 0 && month <= 11 && day >= 1 && day <= 31) {
return new Date(new Date().getFullYear(), month, day)
}
}
}

return new Date(date) // everything else
Expand Down
62 changes: 62 additions & 0 deletions test/issues/issue2844.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import MockDate from 'mockdate'
import dayjs from '../../src'

beforeEach(() => {
MockDate.set(new Date(2023, 0, 1)) // 2023-01-01
})

afterEach(() => {
MockDate.reset()
})

// issue 2844
describe('issue 2844 - Parse MM-DD format dates', () => {
it('should parse MM-DD format with hyphen', () => {
const date = dayjs('10-15')
expect(date.month()).toBe(9) // 10-1 = 9 (October)
expect(date.date()).toBe(15)
expect(date.year()).toBe(2023) // Should use current year
})

it('should parse MM-DD format with slash', () => {
const date = dayjs('10/15')
expect(date.month()).toBe(9) // 10-1 = 9 (October)
expect(date.date()).toBe(15)
expect(date.year()).toBe(2023)
})

it('should parse MM-DD format with dot', () => {
const date = dayjs('10.15')
expect(date.month()).toBe(9) // 10-1 = 9 (October)
expect(date.date()).toBe(15)
expect(date.year()).toBe(2023)
})

it('should validate MM-DD format', () => {
// Valid dates
expect(dayjs('1-1').isValid()).toBe(true)
expect(dayjs('01-01').isValid()).toBe(true)
expect(dayjs('12-31').isValid()).toBe(true)

// Invalid dates
expect(dayjs('13-01').isValid()).toBe(false) // month > 12
expect(dayjs('01-32').isValid()).toBe(false) // day > 31
// Note: Month 0 is considered valid in JavaScript Date (as December of previous year)
expect(dayjs('00-01').isValid()).toBe(true)
expect(dayjs('01-00').isValid()).toBe(false) // day = 0
})

it('should not affect other date formats', () => {
// Full ISO date
const isoDate = dayjs('2022-10-15')
expect(isoDate.year()).toBe(2022)
expect(isoDate.month()).toBe(9)
expect(isoDate.date()).toBe(15)

// YYYY-MM format
const yearMonth = dayjs('2022-10')
expect(yearMonth.year()).toBe(2022)
expect(yearMonth.month()).toBe(9)
expect(yearMonth.date()).toBe(1) // Default to first day of month
})
})