-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
dayjs("2013-11-18T11:55:20").tz("America/Toronto")
does not work
#2102
Comments
I have the same problem on android. |
Same here. NodeJS v16.16.0 and Linux (kernel version: 6.0.5-arch1-1) |
I have the same problem on android. |
Very strange; tried it on windows and on ubuntu 20.04 and in all cases my tests using @tukusejssirs code succeeded (no invalid date). Additionally I created 3 tests in dayjs and got the same results as moment does (even here: no invalid date). My environment was dayjs 1.11.6 on node v14.19.2 on linux, respective node 16.18.0 on windows. |
Okay, I re-tested this with However, whenever I used the system version (installed on Arch Linux via I have no idea what is the difference between them. |
I have the same problem on android. dayjs.extend(utc) dayjs.tz("2013-11-18 11:55:20", "America/Toronto") // '2013-11-18T11:55:20-05:00' please help me . |
@tukusejssirs so this looks more like an issue for yay. @19424056 sorry, but I don't have an android test environment nor did I ever make developments in an android environment. Perhaps the tip from @tukusejssirs helps (installing dayjs via npm). |
@BePo65 i try to install dayjs via npm but not work :( |
The problem appears to be here: dayjs/src/plugin/timezone/index.js Lines 98 to 99 in 9c20e77
The offset is calculated by converting a Date to an en-US locale string, then back again to a Date. However, the conversion back fails in some environments: > new Date().toLocaleString('en-US', { timeZone: "America/Boise" })
'11/18/2022, 1:27:14 PM'
> new Date('11/18/2022, 1:27:14 PM')
Invalid Date This may depend on obscure details of the OS you're running on. For some other locales this works without issue: > new Date().toLocaleString('zh', { timeZone: "America/Boise" })
'2022/11/18 13:28:30'
> new Date('2022/11/18 13:28:30')
2022-11-18T13:28:30.000Z |
Thanks, @galenhuntington! If you are right, I think the solution could be to use /**
* Get timezone offset
*
* @param {Date} date Date
* @param {string} timezone Timezone
* @return {string} Timezone offset
*/
function getTimezoneOffset({date, timezone}) {
return new Intl
.DateTimeFormat('en-US', {timeZone: timezone, timeZoneName: 'longOffset'})
.formatToParts(date)
.find(i => i.type === 'timeZoneName').value.match(/[\d+:-]+$/)?.[0]
}
console.log(getTimezoneOffset({date: new Date('2022-11-18T21:00:00.000Z'), timezone: 'Europe/Bratislava'}))
// Output
// '+01:00' |
The issue is known and tracked there:
NodeJS fix: nodejs/node#45573 |
Describe the bug
dayjs("2013-11-18T11:55:20").tz("America/Toronto")
(as suggested in the docs) outputs the following:Expected behavior
The following should be the output:
Information
1.11.6
,1.11.0
,1.10.0
,1.9.0
(it seems like the bug is in all versions sinceutc
plugin was introduced)America/Toronto
andEurope/Slovakia
; I don’t think it matters;19.0.0
8.19.2
The text was updated successfully, but these errors were encountered: