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

dayjs.tz(time, timezone), when time contains time zone, the result is incorrect #2626

Open
willingfox2 opened this issue Apr 9, 2024 · 2 comments

Comments

@willingfox2
Copy link

Describe the bug
Example: dayjs.tz('2024-04-03T16:21:18+08:00', "America/New_York").format()
Desired result:2024-04-03T04:21:18-04:00
Actual result: 2024-04-03T08:21:18-04:00

Since New York is in the fourth borough in the west, which is 12 hours different from the eighth borough in the east, the result should be 2024-04-03T04:21:18-04:00, but it is not actually like this.

@michaelhillebrand
Copy link

I am having the same issue essentially. I am setting a default timezone and then parsing without the second parameter.

See code example below

const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');

dayjs.extend(utc);
dayjs.extend(timezone);

dayjs.tz.setDefault('America/New_York');
const timestamp = '2024-05-17T10:00:00-08:00';

const test1 = dayjs.tz(timestamp);
console.log('test1.format', test1.format());
// expect  2024-05-17T14:00:00-04:00
// getting 2024-05-17T18:00:00-04:00
console.log('test1.toString', test1.toString());
// expect  Fri, 17 May 2024 18:00:00 GMT
// getting Fri, 17 May 2024 22:00:00 GMT

// This works, but I don't want to have to convert after I parse every time
const test2 = dayjs(timestamp).tz();

console.log('test2.format', test2.format());
// get  2024-05-17T14:00:00-04:00
console.log('test2.toString', test2.toString());
// get  Fri, 17 May 2024 18:00:00 GMT

@IonelLupu
Copy link

I just encountered this issue as well. Here is a playground that replicates the issue: https://codesandbox.io/p/sandbox/dayjs-playground-forked-dy3xwn?file=%2Fsrc%2Findex.js%3A4%2C1

import dayjs from "dayjs";
import timezonePlugin from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";

dayjs.extend(utc);
dayjs.extend(timezonePlugin);

dayjs.tz.setDefault("Asia/Seoul");

console.log(
  dayjs("2024-07-02T00:00:00Z").format("ddd DD MMM YYYY"),
  "default timezone (which should be Asia/Seoul) -> wrong dat"
);

console.log(
  dayjs("2024-07-02T00:00:00Z").tz("Asia/Seoul").format("ddd DD MMM YYYY"),
  "manual timezone (Asia/Seoul) -> correct date"
);

Basically, dayjs.tz.setDefault("Asia/Seoul"); doesn't do anything. We have to manually set the timezone for each of our dates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants