Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 10 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down