iamkun / iamkun/dayjs

`dayjs().tz()` not working as expected with math operations

Open
#1,824 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
`dayjs().tz()`with an operation like `.subtract()`or `.add()` get wrong time, see an example below:

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

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

const initialTime = dayjs()
.tz('utc')
.subtract(15, 'm')
.format('YYYY-MM-DD HH:mm:ss');

const finalTime = dayjs().tz('utc').format('YYYY-MM-DD HH:mm:ss');

console.log(`Timezone: ${dayjs.tz.guess()}`); // output: Timezone: America/Sao_Paulo
console.log(`Initial time: ${initialTime}`); // output: Initial time: 2022-03-10 15:06:15
console.log(`Final time: ${finalTime}`); // output: Final time: 2022-03-10 12:21:15
```

**Expected behavior**
I expected same hour with 15 minutes of difference, something like:
- Initial time: 2022-03-10 12:06:15
- Final time: 2022-03-10 12:21:15

--

I looked at the issues of this library and it seems that there are several unexpected behaviors in relation to `.tz()`,
[Issue that compile bugs](https://github.com/iamkun/dayjs/issues/1805). Do you need any help with this?

**Information**
- Day.js Version: v1.10.8
- OS: macOS Big Sur 11.6.1
- Browser: Not browser, but Node.js v16.13.1

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example with the dayjs/plugin/utc and dayjs/plugin/timezone plugins under Node.js 16. Compare the formatted results from .tz('utc').subtract() and .tz('utc') without arithmetic. Done means arithmetic preserves the UTC display and produces the expected 15-minute difference, with regression coverage added if the repository provides a relevant test location.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.