Moment.js how to use fromNow() to return everything in hours?

前端 未结 2 1654
误落风尘
误落风尘 2020-12-03 19:30

I\'ve searching the moment.js docs and stackoverflow for a way to use the fromNow() function but returning everything in hours.

What I mean is:

2条回答
  •  不思量自难忘°
    2020-12-03 19:48

    If you definitely want to use fromNow(), I don't see any way other than overriding moment's built-in function. For example, you can override it to return the difference in hours as follows:

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

    Then you can check that fromNow() returns the value in hours:

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

    which returns:

    19
    

    Note: Tried at 19:00 :)

提交回复
热议问题