From 2eb2bf3d39ca12c616a424a313658e61ddca7d0a Mon Sep 17 00:00:00 2001 From: Joel Hacke Date: Fri, 1 Mar 2024 12:17:17 -0600 Subject: [PATCH] return null on toISOString if date is invalid --- src/index.js | 2 +- test/display.test.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 061ade178..291c0b041 100644 --- a/src/index.js +++ b/src/index.js @@ -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() { diff --git a/test/display.test.js b/test/display.test.js index ce1f47893..8d62d1953 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -261,6 +261,11 @@ it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => { expect(dayjs().toISOString()).toBe(moment().toISOString()) }) +it('As null -> null', () => { + expect(dayjs(null).toISOString()).toBe(moment(null).toISOString()) + expect(dayjs(null).toISOString()).toBe(null) +}) + it('Year 1 formatted with YYYY should pad with zeroes', () => { const date = new Date(1, 0, 1) date.setUTCFullYear(1) // Required because 0-99 are parsed as 19xx in JS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#year