iamkun / iamkun/dayjs

startOf() and endOf() don't work as expected when utc() is used

Open
#2,122 0 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**
I want to get the last day of the last week of a given month. Similarly, I also want to get the first day of the first week of a given month.
Even though this approach works in local time, it doesn't work when utc() is used.

**Sandbox**: https://codesandbox.io/s/xenodochial-pond-kih9th?file=/src/index.ts

**Expected behavior**
`dayjs('01-01-2022').utc().startOf('month').startOf('week')` should return `2021-12-26`, not `2021-11-29`.
Similarly, `dayjs('01-01-2022').utc().endOf('month').endOf('week')` should return `2022-02-06`, not `2022-01-02`.

**Actual behavior**
(index) | Value
-- | --
start | '2021-12-26T23:00:00.000Z'
end | '2022-02-06T22:59:59.999Z'
startUtc | '2021-11-29T00:00:00.000Z'
endUtc | '2022-01-02T23:59:59.999Z'

Please see the sandbox for more info.
Also - `toISOString()` doesn't work in Firefox or Safari (another bug?!).
So, please open the sandbox in Chrome or replace this method with `format()` or something else.

**Information**
- Day.js Version 1.11.6
- OS: All
- Browser Chrome 107
- Time zone: Central European Standard Time (GMT+1)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the sandbox case with dayjs().utc().startOf() and endOf(), comparing the UTC and local results in Day.js 1.11.6. Trace the UTC plugin and the week/month boundary behavior; done means the stated start and end dates match in UTC, with the reported browser serialization behavior checked separately.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.