dayjs
dayjs copied to clipboard
`dayjs.tz()` throws when parsing invalid date string
Describe the bug
The behavior when parsing an invalid date string differs between dayjs(...), which returns an invalid date, and dayjs.tz(...), which throws RangeError: invalid time value.
Code (available in CodeSandbox):
import dayjs from "dayjs";
import dayjs_utc from "dayjs/plugin/utc";
import dayjs_timezone from "dayjs/plugin/timezone";
dayjs.extend(dayjs_utc);
dayjs.extend(dayjs_timezone);
console.log("Test 1", dayjs(""));
console.log("Test 2", dayjs.tz("", "America/New_York"));
Output:
Test 1 M {$L: 'en', $d: Invalid Date, $x: {…}, $y: NaN, $M: NaN, …}
index.js:27 Error in sandbox:
index.js:27 RangeError: Invalid time value
at DateTimeFormat.formatToParts (<anonymous>)
at a (eval at J (eval.js:31), <anonymous>:1:770)
at u (eval at J (eval.js:31), <anonymous>:1:814)
at eval (eval at J (eval.js:31), <anonymous>:1:1888)
at Function.o.tz (eval at J (eval.js:31), <anonymous>:1:2001)
at $csb$eval (VM737 index.tsx:16)
at J (eval.js:31)
at ee.evaluate (transpiled-module.js:709)
at xe.evaluateTranspiledModule (manager.js:334)
at xe.evaluateModule (manager.js:305)
at compile.ts:724
at c (runtime.js:45)
at Generator._invoke (runtime.js:274)
at Generator.forEach.t.<computed> [as next] (runtime.js:97)
at r (asyncToGenerator.js:3)
at u (asyncToGenerator.js:25)
Expected behavior
dayjs("") and dayjs.tz("", "America/New_York") should behave the same way. They should both return a dayjs object such that isValid() == false.
Information
- Day.js Version: 1.10.7
- OS: Windows 10
- Browser: Chrome 93
- Time zone: GMT-04:00 DST (Eastern Daylight Time)
dayjs('').tz('America/New York') works for me
Just updated the sandbox to the latest dayjs and the behavior is the same (still buggy).
dayjs('').tz('America/New York')works for me
dayjs(value).tz(...) will not treat value to be in the local timezone. so this workaround only works for the current time, not for parsing existing times.