admin管理员组

文章数量:1431002

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);

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);
Share Improve this question edited Jun 20, 2013 at 18:41 Staysee asked Jun 20, 2013 at 17:05 StayseeStaysee 1,9075 gold badges23 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7
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());
}

本文标签: javascriptMomentjs only within the last weekStack Overflow