Skip to content

Commit

Permalink
fix: Update Regex to parse 'YYYY' correctly (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyoshikawa committed Jul 29, 2020
1 parent b6cfa84 commit 70c1239
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
export const INVALID_DATE_STRING = 'Invalid Date'

// regex
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d{1,3})?$/
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})?-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d{1,3})?$/
export const REGEX_FORMAT = /\[([^\]]+)]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ const parseDate = (cfg) => {
if (typeof date === 'string' && !/Z$/i.test(date)) {
const d = date.match(C.REGEX_PARSE)
if (d) {
const m = d[2] - 1 || 0
if (utc) {
return new Date(Date.UTC(d[1], d[2] - 1, d[3]
return new Date(Date.UTC(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0))
}
return new Date(d[1], d[2] - 1, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)
return new Date(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)
}
}

Expand Down
9 changes: 7 additions & 2 deletions test/plugin/utc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ describe('Parse UTC ', () => {

it('Parse date string without timezome', () => {
const d = '2018-09-06'
const d2 = '2018-09'
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
expect(dayjs.utc(d).format()).toEqual('2018-09-06T00:00:00Z')
expect(dayjs(d).utc().format()).toEqual(moment(d).utc().format())
const d2 = '2018-09'
expect(dayjs.utc(d2).format()).toEqual(moment.utc(d2).format())
expect(dayjs.utc(d2).format()).toEqual('2018-09-01T00:00:00Z')
expect(dayjs(d).utc().format()).toEqual(moment(d).utc().format())
expect(dayjs(d2).utc().format()).toEqual(moment(d2).utc().format())
const d3 = '2018'
expect(dayjs.utc(d3).format()).toEqual(moment.utc(d3).format())
expect(dayjs.utc(d3).format()).toEqual('2018-01-01T00:00:00Z')
expect(dayjs(d3).utc().format()).toEqual(moment(d3).utc().format())
})

it('creating with utc with timezone', () => {
Expand Down

0 comments on commit 70c1239

Please sign in to comment.