Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i compare date like moment.duration #414

Closed
Evansy opened this issue Nov 20, 2018 · 14 comments
Closed

how can i compare date like moment.duration #414

Evansy opened this issue Nov 20, 2018 · 14 comments

Comments

@Evansy
Copy link

Evansy commented Nov 20, 2018

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,
    ....
}
@naulacambra
Copy link
Contributor

Check #122

@Evansy
Copy link
Author

Evansy commented Nov 21, 2018

@naulacambra
I've tried it, but it doesn't seem to fit my needs...

@iamkun
Copy link
Owner

iamkun commented Nov 21, 2018

Update:

Duration
https://day.js.org/docs/en/durations/durations


have you tried RelativeTime plugin?

@Evansy
Copy link
Author

Evansy commented Nov 28, 2018

have you tried RelativeTime plugin?

yes, It' can't.

@naulacambra
Copy link
Contributor

Can you show the code with RelativeTime plugin that you've tried?

@Newbie012
Copy link

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')

@iamkun
Copy link
Owner

iamkun commented Feb 25, 2019

@Newbie012 How does momentjs do this?

@Newbie012
Copy link

Newbie012 commented Feb 25, 2019

@iamkun I haven't used momentjs much in my projects since it's a one-hell-of-a-package :D, so I'm not sure exactly.

I don't think MomentJS does it that way.

@iamkun
Copy link
Owner

iamkun commented Feb 25, 2019

@Newbie012 Yes, I don't know much about moment.duration, and not sure what kind of plugin / API should we provide.

@Newbie012
Copy link

Currently, I haven't found a lightweight maintained package that offers this kind of feature, so I guess it's up to you whether to implement this kind of plugin / API :)

@iamkun
Copy link
Owner

iamkun commented Feb 25, 2019

It's a nice to have feature, why not. 😁 Discussion welcome.

@iamkun
Copy link
Owner

iamkun commented Apr 9, 2019

#564 todo: Duration plugin

@l-7-l
Copy link

l-7-l commented 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}分` : '';

@millievn
Copy link

As it said As with the other getters for durations, dayjs.duration().hours() gets the hours (0 - 23)., it there anyway to show only hours without days like 30 hours 40 minutes to show time down??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants