iamkun / iamkun/dayjs

Timezone startOf issue that involve UTC and daylight savings time

Open
#1,469 0 comments 5 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**
The behavior of `dayjs.utc("2021-03-01").startOf("month")` and `dayjs.tz("2021-03-01", "UTC").startOf("month")` is different

**Expected behavior**
```
const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
const timezone = require("dayjs/plugin/timezone");

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

// const today = new Date("2021-03-01");
const today = "2021-03-01"

const isoDate01 = dayjs.utc(today).startOf("month").toISOString();
const isoDate02 = dayjs.tz(today, "UTC").startOf("month").toISOString();

console.log(isoDate01);
console.log(isoDate02);
```

console.log result
```
2021-03-01T00:00:00.000Z
2021-02-28T23:00:00.000Z
```

Expected behavior
```
2021-03-01T00:00:00.000Z
2021-03-01T00:00:00.000Z
```

Does anyone know when should we use dayjs.utc(today) or dayjs.tz(today, "UTC")?
Why they behave differently?

**Information**
- Day.js Version [e.g. v1.10.4]
- OS: iOS
- Time zone: [e.g. GMT-07:00 DST (Pacific Daylight Time)]

Contributor guide

Open the contributing guide

Research direction

Start with the supplied reproduction and inspect the dayjs/plugin/utc and dayjs/plugin/timezone entry points, focusing on how startOf("month") handles UTC and daylight-saving offsets. Confirm the differing toISOString() results, then add or update coverage so both UTC forms produce the expected 2021-03-01T00:00:00.000Z result.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
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.