iamkun / iamkun/dayjs

Random answer in .diff function with truncate option

Open
#1,618 1 comment 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**
A clear and concise description of what the bug is.

**Expected behavior**
A clear and concise description of what you expected to happen.

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

Calculating the difference in days between 2 dates returns an unexpected behavior sometimes. The Truncate option is working ok, but the difference is having floating point issues i think during the calculation of the difference.

`let yesterday = dayjs().tz('America/Sao_Paulo').subtract(1, 'day')`
`
let day_180 = dayjs().tz('America/Sao_Paulo').subtract(5, 'month').add(1, 'day')`
`let nb_days = yesterday.diff(day_180, 'day', true)`
`console.log("nbdays: "+nb_days+" yesterday: "+yesterday.format())
`

![image](https://user-images.githubusercontent.com/25071633/131671214-3e4b8414-099a-42ce-ae33-8b1024d953d0.png)

Therefore, sometimes the result is 150 and sometimes 151 after truncation as above;

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the reported .diff calculation with the America/Sao_Paulo timezone, comparing repeated results for the same dates and truncation settings. Trace the date-difference path used by .diff and verify the fix against the example, ensuring truncation consistently produces the expected day count.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.