Random answer in .diff function with truncate option
- 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())
`

Therefore, sometimes the result is 150 and sometimes 151 after truncation as above;
Contributor 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