iamkun / iamkun/dayjs

Using dayjs.tz.setDefault with endOf('day') makes the date tomorrow

Open
#1,327 2 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
48.7k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**

If I set the global timezone with `dayjs.tz.setDefault()` and use `dayjs.tz()` with `endOf('day')` I get the next day.

- 2020-01-**08**T23:05:05-08:00 (date I used)
- 2020-01-**09**T23:59:59-08:00 (date I get)
- 2020-01-**08**T23:59:59-08:00 (date I should get)

[Example Code](https://codesandbox.io/s/dayjstzsetdefault-bug-f5ju6?file=/src/index.ts)

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

dayjs.tz.setDefault("America/Los_Angeles");

dayjs.tz("2020-01-08T23:05:05-08:00").endOf("day").format(); // 2020-01-09T23:59:59-08:00 (wrong date)
```

Using `dayjs().tz().endOf("day")` works correctly but not `dayjs.tz().endOf("day")`:

```ts
// This works correctly:
dayjs("2020-01-08T23:05:05-08:00").tz("America/Los_Angeles").endOf("day").format(); // 2020-01-08T23:59:59-08:00

// This does not work correctly (same code above in the first example)
dayjs.tz.setDefault("America/Los_Angeles");
dayjs.tz("2020-01-08T23:05:05-08:00").endOf("day").format(); // 2020-01-09T23:59:59-08:00 (wrong date)
```

**Expected behavior**

I expect the end of day be the same of the date being passed in.

Also `startOf("day")` causes the same issue so I don't think it's only related to `endOf("day")` and `startOf("day")`.

**Information**
- Day.js Version 1.10.3
- OS: macOS Catalina 10.15.7
- Browser Google Chrome Version 87.0.4280.88
- Time zone: GMT-06:00 DST (Central Daylight Time)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the discrepancy using the linked CodeSandbox and the dayjs.tz(), startOf('day'), and endOf('day') entry points. Compare the default-timezone path with dayjs().tz() and verify that both start and end of day preserve the input date in the expected timezone.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.