dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

`dayjs.tz()` throws when parsing invalid date string

Open srmagura opened this issue 4 years ago • 3 comments

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)

srmagura avatar Sep 19 '21 17:09 srmagura

dayjs('').tz('America/New York') works for me

epszaw avatar May 27 '22 12:05 epszaw

Just updated the sandbox to the latest dayjs and the behavior is the same (still buggy).

srmagura avatar May 27 '22 13:05 srmagura

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.

mstykow avatar Aug 29 '22 10:08 mstykow