Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js only within the last week

I am using moment.js "timeAgo" to display some dates. But I only want to use moment.js if the date is within the last week. (7 days ago) After that point I would just like to display the date as normal. Example: (Jun 20, 2010) instead of (3 years ago).

I have the defaults working with this code. Any help would be great.

var date = moment.unix(<?php echo $date; ?>).fromNow();
$("#date").append(date);
like image 589
Staysee Avatar asked Mar 23 '23 11:03

Staysee


1 Answers

var someDate = moment.unix(<?php echo $date; ?>);
if(moment().diff(someDate, 'days') > 7){
    $("#date").append(someDate.format("dddd, MMMM Do YYYY"));
} else {
    $("#date").append(someDate.timeAgo());
}
like image 97
Brian Lewis Avatar answered Apr 07 '23 04:04

Brian Lewis