Moment.js, how to use fromNow () to return everything in a watch?

I was looking for moment.js docs and /qaru.site / ... for using a function fromNow(), but returning everything in a clock.

What I mean:

moment([2017, 01, 05]).fromNow();     // a day ago

it should be

moment([2017, 01, 05]).fromNow();     // 24 hours ago

I know that this can be done using .diffand, possibly, other similar functions, and then adding text, but can it be used .fromNow()for this?

+2
source share
2 answers

You can use relativeTimeThresholdto set thresholds for a relative point in time.

As the docs say:

duration.humanize , , , . , 45 , 22 ​​ . , moment.relativeTimeThreshold(unit, limit), - s, m, h, d, m.

, . , 1 26 :

var m1 = moment().subtract(5, 'h');
var m2 = moment().subtract(55, 'h');
var m3 = moment().subtract(1, 'd');

// Default results
console.log(m1.fromNow());
console.log(m2.fromNow());
console.log(m3.fromNow());

// Change relativeTimeThreshold
moment.relativeTimeThreshold('m', 60);
moment.relativeTimeThreshold('h', 24*26);

// Results in hours
console.log(m1.fromNow());
console.log(m2.fromNow());
console.log(m3.fromNow());
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

, , relativeTime ( ) relativeTimeRounding.

+6

fromNow(), , . , , :

moment.fn.fromNow = function (a) {
    var duration = moment().diff(this, 'hours');
    return duration;
}

, fromNow() :

console.log(moment([2017,0,6]).fromNow());  

:

19

: 19:00:)

+1

Source: https://habr.com/ru/post/1680665/


All Articles