Peter
Peter

Reputation: 11835

Timeago date format for less than 24h ago only

I would like to use the jquery Timeago plugin as a date format. However, when the time ago has exceeded 24h, I would like it to show another format.

I tried to tweak the code, but my skills let me down :/

EDIT:

var itemDate = '2014-05-17T09:24:17Z';

if(itemDate > 'THAN 24 HOURS')
{
   $('body').append('<div>show normal date like 17.05.2014</div>');
}
else
{
   $('body').append('<time class="timeago" datetime="'+itemDate+'"></time> ');  
   $(".timeago").timeago();
}

Upvotes: 1

Views: 955

Answers (1)

hutchbat
hutchbat

Reputation: 796

You can create a function that check if the date is 24h older or not .. may be something like:

function is_older_than_24hours(datetime) {
     var before = new Date(datetime),
         now = new Date();
     return ( ( now - before ) > ( 1000 * 60 * 60 * 24 )  ) ? true : false;
}

// Then you use your usual code

$(function(){

    var itemDate = '2014-05-17T09:24:17Z';

    if(is_older_than_24hours(itemDate)) {

        var d = new Date(itemDate),
            day = ( d.getDate() > 9 ) ? d.getDate() : '0' + d.getDate(),
            month = ( d.getMonth() > 8 ) ? d.getMonth() + 1 : '0' + (d.getMonth() + 1),
            year = d.getFullYear();
        $('<div/>', { text: month +'.'+ day +'.'+ year }).appendTo('body');

    } else {

        $('<time/>', { 'class': 'timeago', 'datetime': itemDate })
            .appendTo('body')
            .timeago();
    }

})

Upvotes: 7

Related Questions