How to use Moment.js?

前端 未结 4 1244
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 22:09

I\'m unable to follow the Moment.js documentation, and need some help with setting it up. I\'ve referenced the moment.min.js file properly on my webpage like so

4条回答
  •  抹茶落季
    2020-12-04 22:50

    Thanks to @JohannesKlauß for the code. This is basically how I executed his answer and how I am referencing the code on my website.

    
    
    

    Where, moment.min.js is the Moment.js library, and moment.executor.js has this code (courtesy of Johannes):

    jQuery(document).ready(function($){
    
        var now = moment();
    
        $('time').each(function(i, e) {
            var time = moment($(e).attr('datetime'));
    
            if(now.diff(time, 'days') <= 1) {
                $(e).html('' + time.from(now) + '');
            }
        });
    
    });
    

    PS: You can actually edit moment.min.js and add the aforementioned code right in it, at the end. This way, you will be saving one additional HTTP request. :P

提交回复
热议问题