Skip to content

Commit 231790d

Browse files
authored
fix: fix parse date string error e.g. '2020/9/30' (#980)
fix #979
1 parent 90c98e4 commit 231790d

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/constant.js

Lines changed: 1 addition & 1 deletion
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{1,3})?$/
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{1,3})?$/
3030
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

test/parse.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import moment from 'moment'
22
import MockDate from 'mockdate'
33
import dayjs from '../src'
4+
import { REGEX_PARSE } from '../src/constant'
45

56
beforeEach(() => {
67
MockDate.set(new Date())
@@ -125,3 +126,12 @@ it('Clone with same value', () => {
125126
const another = newBase.clone()
126127
expect(newBase.toString()).toBe(another.toString())
127128
})
129+
130+
describe('REGEX_PARSE', () => {
131+
it('2020/9/30', () => {
132+
const date = '2020/9/30'
133+
const d = date.match(REGEX_PARSE)
134+
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
135+
expect(d.join('-')).toBe('2020/9/30-2020-9-30----')
136+
})
137+
})

0 commit comments

Comments
 (0)