Skip to content

Commit

Permalink
fix: fix parse date string error e.g. '2020/9/30' (#980)
Browse files Browse the repository at this point in the history
fix #979
  • Loading branch information
iamkun committed Aug 3, 2020
1 parent 90c98e4 commit 231790d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/constant.js
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
10 changes: 10 additions & 0 deletions test/parse.test.js
@@ -1,6 +1,7 @@
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
import { REGEX_PARSE } from '../src/constant'

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -125,3 +126,12 @@ it('Clone with same value', () => {
const another = newBase.clone()
expect(newBase.toString()).toBe(another.toString())
})

describe('REGEX_PARSE', () => {
it('2020/9/30', () => {
const date = '2020/9/30'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2020/9/30-2020-9-30----')
})
})

0 comments on commit 231790d

Please sign in to comment.