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

Modifying (.add(), .subtract(), etc.) a dayjs instance created in UTC corrupts it #2037

Open
marnixhoh opened this issue Aug 23, 2022 · 5 comments

Comments

@marnixhoh
Copy link

marnixhoh commented Aug 23, 2022

Setting timezone explicitly

dayjs.tz(0, 'UTC').format()

'1970-01-01T00:00:00Z' - Result
'1970-01-01T00:00:00Z' - Expected


dayjs.tz(0, 'UTC').add(0,'minutes').format()

'1969-12-31T23:00:00Z' - Result
'1970-01-01T00:00:00Z' - Expected


dayjs.tz(0, 'UTC').add(1,'minutes').format()

'1969-12-31T23:01:00Z' - Result
'1970-01-01T00:01:00Z' - Expected

Setting default timezone

The above results are identical, when omitting 'UTC' and setting UTC as default:

dayjs.tz.setDefault('UTC')

dayjs.tz(0).format()

'1970-01-01T00:00:00Z' - Result
'1970-01-01T00:00:00Z' - Expected


dayjs.tz(0).add(0,'minutes').format()

'1969-12-31T23:00:00Z' - Result
'1970-01-01T00:00:00Z' - Expected


dayjs.tz(0).add(1,'minutes').format()

'1969-12-31T23:01:00Z' - Result
'1970-01-01T00:01:00Z' - Expected


Information

  • Day.js Version 1.11.5
  • OS: Mac
  • Browser: Chrome
@BePo65
Copy link
Contributor

BePo65 commented Oct 17, 2022

I could reproduce the issue; working on a solution / pr.

@maulik1729
Copy link

maulik1729 commented Oct 28, 2022

@BePo65 found any solution for this?
Same problem for:
dayjs("2022-09-28").tz("UTC", true).format("YYYY-MM-DD HH:mm:ss");
moment("2022-09-28").tz("UTC", true).format("YYYY-MM-DD HH:mm:ss");
Giving different output.

Thanks.

@BePo65
Copy link
Contributor

BePo65 commented Oct 28, 2022

Yes I tried every combination and found that the problem is the utc time zone (even as gmt e.a.) I am working on it for the last 2 weeks, but i could not yet solve all combinations. At the moment I just have to fix the .tz('UTC') variant.

Please be patient as it will take me for sure a few more days; but I won't surrender 😃

@maulik1729
Copy link

maulik1729 commented Oct 30, 2022

dayjs(data).tz('UTC', true).format('YYYY-MM-DD HH:mm:ss'): incorrect output
dayjs(data).utc(true).format('YYYY-MM-DD HH:mm:ss'): correct output

@BePo65 this gives different output shouldn't we use utc implementation when timezone has offset of zero?

proto.tz = function (timezone = defaultTimezone, keepLocalTime) { 
        const oldOffset = this.utcOffset();
        const date = this.toDate();
        const target = date.toLocaleString("en-US", { timeZone: timezone });
        const diff = Math.round((date - new Date(target)) / 1000 / 60);
        const offset = -Math.round(date.getTimezoneOffset() / 15) * 15 - diff;
        if(offset === 0){                           
            return this.utc(keepLocalTime);    //Please check this    
        }
        let ins = d(target)
            .$set(MS, this.$ms)
            .utcOffset(offset, true);
        if (keepLocalTime) {
            const newOffset = ins.utcOffset();
            ins = ins.add(oldOffset - newOffset, MIN);
        }
        ins.$x.$timezone = timezone;
        return ins;
    };

@BePo65
Copy link
Contributor

BePo65 commented Nov 2, 2022

good idea; trying to use it in my pr.

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