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: Fix CustomParseFormat plugin of parsing only YYYY / YYYY-MM bug #873

Merged
merged 1 commit into from
Apr 17, 2020
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
15 changes: 7 additions & 8 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,20 @@ const parseFormattedInput = (input, format, utc) => {
const {
year, month, day, hours, minutes, seconds, milliseconds, zone
} = parser(input)
if (zone) {
return new Date(Date.UTC(
year, month - 1, day,
hours || 0,
minutes || 0, seconds || 0, milliseconds || 0
) + (zone.offset * 60 * 1000))
}
const now = new Date()
const d = day || ((!year && !month) ? now.getDate() : 1)
const y = year || now.getFullYear()
const M = month > 0 ? month - 1 : now.getMonth()
let M = 0
if (!(year && !month)) {
M = month > 0 ? month - 1 : now.getMonth()
}
const h = hours || 0
const m = minutes || 0
const s = seconds || 0
const ms = milliseconds || 0
if (zone) {
return new Date(Date.UTC(y, M, d, h, m, s, ms + (zone.offset * 60 * 1000)))
}
if (utc) {
return new Date(Date.UTC(y, M, d, h, m, s, ms))
}
Expand Down
19 changes: 19 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ it('parse HH:mm:ss but only one digit', () => {
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})

describe('parse YYYY / YYYY-MM only', () => {
it('YYYY', () => {
const input = '2001 +08:00'
const format = 'YYYY Z'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '2001'
const format2 = 'YYYY'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
it('YYYY-MM', () => {
const input = '2001-01 +08:00'
const format = 'YYYY-MM Z'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '2001-01'
const format2 = 'YYYY-MM'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
})

it('parse hh:mm:ss but only one digit', () => {
const input = '0:0:1'
const format = 'hh:mm:ss'
Expand Down