Skip to content

Commit 64037e6

Browse files
bcurtin144iamkun
authored andcommitted
fix: escape last period to match only milliseconds (#1239) (#1295)
1 parent f63375d commit 64037e6

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/constant.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
2626
export const INVALID_DATE_STRING = 'Invalid Date'
2727

2828
// regex
29-
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d+)?$/
29+
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?\.?(\d+)?$/
3030
export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,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

test/parse.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,34 @@ describe('REGEX_PARSE', () => {
148148
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
149149
expect(d.join('-')).toBe('2019-03-25T06:41:00.999999999-2019-03-25-06-41-00-999999999')
150150
})
151+
it('20210102T012345', () => {
152+
const date = '20210102T012345'
153+
const d = date.match(REGEX_PARSE)
154+
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
155+
expect(d.join('-')).toBe('20210102T012345-2021-01-02-01-23-45-')
156+
})
157+
it('2021-01-02T01:23', () => {
158+
const date = '2021-01-02T01:23'
159+
const d = date.match(REGEX_PARSE)
160+
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
161+
expect(d.join('-')).toBe('2021-01-02T01:23-2021-01-02-01-23--')
162+
})
163+
it('2021-01-02T01:23:45', () => {
164+
const date = '2021-01-02T01:23:45'
165+
const d = date.match(REGEX_PARSE)
166+
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
167+
expect(d.join('-')).toBe('2021-01-02T01:23:45-2021-01-02-01-23-45-')
168+
})
169+
it('2021-01-02T01:23:45-0500 (no regex match)', () => {
170+
const date = '2021-01-02T01:23:45-0500'
171+
const d = date.match(REGEX_PARSE)
172+
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
173+
expect(d).toBe(null)
174+
})
175+
it('2021-01-02T01:23:45Z (no regex match)', () => {
176+
const date = '2021-01-02T01:23:45Z'
177+
const d = date.match(REGEX_PARSE)
178+
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
179+
expect(d).toBe(null)
180+
})
151181
})

0 commit comments

Comments
 (0)