Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong ms conversion #2771

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ 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 msLength = 3
const ms = (d[7] || '0').substring(0, msLength).padEnd(msLength, '0')
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
34 changes: 34 additions & 0 deletions test/issues/issue2769.wrong-ms-conversion.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import MockDate from 'mockdate'
import dayjs from '../../src'

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

describe('issue 2769 - wrong ms conversion', () => {
it('should correctly convert date string without timezone', () => {
const ds1 = '2024-11-18T02:04:48'
expect(dayjs(ds1).valueOf()).toEqual(new Date(ds1).valueOf())
const ds2 = '2024-11-18T02:04:48.3'
expect(dayjs(ds2).valueOf()).toEqual(new Date(ds2).valueOf())
const ds3 = '2024-11-18T02:04:48.19'
expect(dayjs(ds3).valueOf()).toEqual(new Date(ds3).valueOf())
const ds4 = '2024-11-18T02:04:48.195'
expect(dayjs(ds4).valueOf()).toEqual(new Date(ds4).valueOf())
})

it('should correctly convert date string with timezone', () => {
const ds1 = '2024-11-18T02:04:48Z'
expect(dayjs(ds1).valueOf()).toEqual(new Date(ds1).valueOf())
const ds2 = '2024-11-18T02:04:48.3Z'
expect(dayjs(ds2).valueOf()).toEqual(new Date(ds2).valueOf())
const ds3 = '2024-11-18T02:04:48.19Z'
expect(dayjs(ds3).valueOf()).toEqual(new Date(ds3).valueOf())
const ds4 = '2024-11-18T02:04:48.195Z'
expect(dayjs(ds4).valueOf()).toEqual(new Date(ds4).valueOf())
})
})