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.

how can i compare date like moment.duration

See original GitHub issue

How can i get a result like below by dayjs?

const diff = moment.duration(moment("2018-01-01").diff(moment("2019-5-12 10:15:23")));
console.log(diff._date);
// {
    years: 1,
    months: 5,
    days: 12,
    hours: 10,
    ....
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
l-7-lcommented, Jul 11, 2019

@see https://github.com/huangjinlin/dayjs-precise-range

   dayjs.extend(preciseDiff);

    const duration = dayjs.preciseDiff(dayjs(timeA), dayjs(timeB), true);
    const years = duration.years;
    const year = years ? `${years}年` : '';
    const months = duration.months;
    const month = months ? `${months}月` : '';
    const days = duration.days;
    const day = days ? `${days}天` : '';
    const hours = duration.hours;
    const hour = hours ? `${hours}小时` : '';
    const minutes = duration.minutes;
    const minute = minutes ? `${minutes}分` : '';

1reaction
Newbie012commented, Feb 25, 2019

Currently, I’m using RelativeTime to achieve that, but it’s not perfect:

dayjs(milliseconds).locale('he').from(0, true)

The problem here is that it will show us only years or days or hours etc… It’ll never show something like 2 Hours and 23 Minutes. It would’ve been super if we could have a plugin that handles that, with an option of how “deep” do we want to be specific (e.g. be specific until minutes), Maybe something like:

dayjs().duration(milliseconds).until('minutes')
Read more comments on GitHub >

github_iconTop Results From Across the Web

Moment js date time comparison - Stack Overflow
Here, I am comparing two dates i.e. 2014-03-24T01:15:000 > 2014-03-24T01:14:000 , expecting that the first one is greater than the second one, ...
Read more >
How to compare two dates with JavaScript? - Tutorialspoint
Moment. js has a diff() method, which gives the difference between two dates in years, months, days, hours, minutes, seconds, milliseconds, etc ...
Read more >
Difference - momentjs.com - Read the Docs
See the docs on moment#duration for more info. The supported measurements are years , months , weeks , days , hours , minutes...
Read more >
How to Compare the Date Part of a Date Object Only Without ...
The moment.js library has the isAfter method that lets us compare 2 date-times without the time part. To do this, we write:
Read more >
Docs - Moment.js
The Date object internally represents a Unix timestamp with millisecond precision. It offers functions that will convert to and from the system's local...
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