question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duration between dates seems to be incorrect

See original GitHub issue

Describe the bug Duration seems to be incorrect. The difference between March 29th, 2020 and March 28th 2021 should be less than 12 months

Expected behavior Calculating the difference between two days one year apart from each other should yield a difference of one year.

Information

  • Day.js Version: 1.10.3
  • OS: Windows 10 20H2
  • Browser Chrome 89
  • Time zone: GMT -4:00

I’m trying to get the difference between two dates:

  • A fixed date (March 29th, 2020)
  • Todays date (at the time of writing March 28th, 2021)

I do that with:

      const lockdown = dayjs("3/29/2020", "M/DD/YYYYY"); //29th March 2020
      const currentDate = dayjs();
 
      // get the difference between the moments
      const diff = currentDate.diff(lockdown);
    
      //express as a duration
      const diffDuration = dayjs.duration(diff);

   
      // display
      const timeInLockDown = {
        months: diffDuration.months(),
        days: diffDuration.days(),
        hours: diffDuration.hours(),
        minutes: diffDuration.minutes(),
        seconds: diffDuration.seconds()
      };

This yields diffDuration as:

days: 4
hours: 1
milliseconds: 709
minutes: 4
months: 12
seconds: 5
years: 0

Which doesn’t make sense to me? It shouldn’t 12 months and 4 days. Also, I think that if it is 12 months and 4 days, then years should be 1 but in this instance it’s 0.

Is this expected behaviour?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

7reactions
avandcommented, May 27, 2021

I just noticed a bug and it seems related to this issue:

// dayjs version 1.10.5
import dayjs from 'dayjs';
import dayjsDurationPlugin from 'dayjs/plugin/duration';

const a = dayjs('3/19/2018');
const b = dayjs('3/17/2021');
const diff = b.diff(a, 'day'); // => 1094 correct
dayjs.duration(diff, 'days'); // => { years: 2, months: 12, days: 4 } incorrect

I would expect the output to be { years: 3, days: 4 }.

0reactions
guypurseycommented, Aug 28, 2022

Further evidence for the above observed in comment on a related issue.

Brief thread on more accurate duration requirements also adds to this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to correct a #VALUE! error in the DAYS function
Solution: Check your system's date and time settings to make sure that they match the format of the dates referred to in the...
Read more >
Getting wrong time after calculating difference between two ...
I want to calculate the difference between a certain date and the current time. ... I want to get the amount of days,...
Read more >
Zap dates or times are incorrect - Zapier Help
Sometimes you might just want to adjust your date or time by a consistent interval, for instance always seven days ahead of the...
Read more >
How do I calculate the interval between two dates?
FAQ: Calculate Interval between Dates (v1.0, 11/15/20) ... can use the datediff function in a calculated field to compute the interval (days, weeks,...
Read more >
Excel DATEDIF function to get difference between two dates
To find out how many weeks there are between two dates, you can use the DATEDIF function with "D" unit to return the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found