Skip to content

Commit d1bdd36

Browse files
authored
fix: support parsing unlimited decimals of millisecond (#1010)
fix #544
1 parent 5a465cc commit d1bdd36

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
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{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+)?$/
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

src/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ const parseDate = (cfg) => {
6060
const d = date.match(C.REGEX_PARSE)
6161
if (d) {
6262
const m = d[2] - 1 || 0
63+
const ms = (d[7] || '0').substring(0, 3)
6364
if (utc) {
6465
return new Date(Date.UTC(d[1], m, d[3]
65-
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0))
66+
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms))
6667
}
6768
return new Date(d[1], m, d[3]
68-
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)
69+
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
6970
}
7071
}
7172

test/parse.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ describe('Parse', () => {
8181
expect(normalized.toISOString()).toEqual(expected)
8282
})
8383

84+
it('parses unlimited millisecond', () => {
85+
const date = '2019-03-25T06:41:00.999999999'
86+
const ds = dayjs(date)
87+
const ms = moment(date)
88+
expect(ds.valueOf()).toEqual(ms.valueOf())
89+
expect(ds.millisecond()).toEqual(ms.millisecond())
90+
})
91+
8492
it('String Other, Null and isValid', () => {
8593
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
8694
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())
@@ -134,4 +142,10 @@ describe('REGEX_PARSE', () => {
134142
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
135143
expect(d.join('-')).toBe('2020/9/30-2020-9-30----')
136144
})
145+
it('2019-03-25T06:41:00.999999999', () => {
146+
const date = '2019-03-25T06:41:00.999999999'
147+
const d = date.match(REGEX_PARSE)
148+
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
149+
expect(d.join('-')).toBe('2019-03-25T06:41:00.999999999-2019-03-25-06-41-00-999999999')
150+
})
137151
})

test/plugin/utc.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ describe('Parse UTC ', () => {
9494
expect(dayjs.utc(d).format()).toEqual('2018-09-06T19:34:28Z')
9595
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
9696
})
97+
98+
it('parses unlimited millisecond in utc', () => {
99+
const date = '2019-03-25T06:41:00.999999999'
100+
const ds = dayjs.utc(date)
101+
const ms = moment.utc(date)
102+
expect(ds.valueOf()).toEqual(ms.valueOf())
103+
expect(ds.millisecond()).toEqual(ms.millisecond())
104+
})
97105
})
98106

99107
it('Clone retains the UTC mode', () => {

0 commit comments

Comments
 (0)