Asa Carter
Asa Carter

Reputation: 2225

Customise to moment.js fromNow above a specified period

I'm using moment.js to display the relative time:

<time datetime="2014-01-29 17:06:07">29 January 2014 15:06pm</time>

$('time').each(function(i, e) {
    var time = moment($(e).attr('datetime'));
    $(e).find('span').html(time.fromNow());
});

How can I customise it to display the full date and time if the time lapsed is past a specified period such as 1 month?

Upvotes: 0

Views: 178

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241485

$('time').each(function(i, e) {
    var time = moment($(e).attr('datetime'));

    var s = time.isBefore(moment().subtract(1,'months'))
              ? moment.format() // optionally pass a format specifier if you like
              : time.fromNow();

    $(e).find('span').html(s);
});

Upvotes: 1

Related Questions