From bbedc071fcdac12b4e05bcf76a49a261a1b67b01 Mon Sep 17 00:00:00 2001 From: lenamonj Date: Wed, 29 Jul 2026 11:27:57 -0400 Subject: [PATCH] fix: parse fractional seconds shorter than 3 digits as a decimal fraction --- src/index.js | 2 +- test/parse.test.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 061ade178..d2634acad 100644 --- a/src/index.js +++ b/src/index.js @@ -69,7 +69,7 @@ const parseDate = (cfg) => { const d = date.match(C.REGEX_PARSE) if (d) { const m = d[2] - 1 || 0 - const ms = (d[7] || '0').substring(0, 3) + const ms = `${d[7] || '0'}00`.substring(0, 3) // '.5' is 500ms: pad before truncating if (utc) { return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)) diff --git a/test/parse.test.js b/test/parse.test.js index 9f2dedf2d..c649d78b6 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -89,6 +89,16 @@ describe('Parse', () => { expect(ds.millisecond()).toEqual(ms.millisecond()) }) + it('parses millisecond with fewer than 3 digits as a decimal fraction', () => { + const dates = ['2019-03-25T06:41:00.5', '2019-03-25T06:41:00.05', '2019-03-25T06:41:00.005'] + dates.forEach((date) => { + const ds = dayjs(date) + const ms = moment(date) + expect(ds.valueOf()).toEqual(ms.valueOf()) + expect(ds.millisecond()).toEqual(ms.millisecond()) + }) + }) + it('String Other, Undefined and Null and isValid', () => { global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())