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

Handle the invalid date the same way moment.js does, with and without the utc plugin #2574

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
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ class Dayjs {
// ie 8 return
// new Dayjs(this.valueOf() + this.$d.getTimezoneOffset() * 60000)
// .format('YYYY-MM-DDTHH:mm:ss.SSS[Z]')
return this.$d.toISOString()
return this.isValid() ? this.$d.toISOString() : null
}

toString() {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default (option, Dayjs, dayjs) => {
}

proto.toISOString = function () {
return this.toDate().toISOString()
return this.isValid() ? this.toDate().toISOString() : null
}

proto.toString = function () {
Expand Down
14 changes: 14 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ describe('Parse', () => {
expect(dayjs('otherString').isValid()).toBe(false)
expect(dayjs(null).toString().toLowerCase()).toBe(moment(null).toString().toLowerCase())
})

describe('Handles an invalid date in the same way that moment.js does', () => {
it('returns null for invalid date', () => {
expect(JSON.stringify({ date: dayjs('invalid') })).toEqual(JSON.stringify({ date: null }))
})

it('returns string "Invalid Date" when calling toString', () => {
expect(JSON.stringify({ date: dayjs('invalid').toString() })).toEqual(JSON.stringify({ date: 'Invalid Date' }))
})

it('returns null when calling toISOString', () => {
expect(JSON.stringify({ date: dayjs('invalid').toISOString() })).toEqual(JSON.stringify({ date: null }))
})
})
})

it('Unix Timestamp Number (milliseconds) 1523520536000', () => {
Expand Down
6 changes: 6 additions & 0 deletions test/plugin/utc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,9 @@ it('utc keepLocalTime', () => {
it('utc diff undefined edge case', () => {
expect(dayjs().diff(undefined, 'seconds')).toBeDefined()
})

describe('Handles an invalid date in the same way that moment.js does', () => {
it('returns null when calling toISOString', () => {
expect(dayjs('invalid').toISOString()).toBeNull()
})
})
Loading