Moment.js仅在上周内 [英] Moment.js only within the last week

查看:70
本文介绍了Moment.js仅在上周内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用moment.jstimeAgo来显示一些日期。但是如果日期在上周内,我只想使用moment.js。 (7天前)在那之后我只想正常显示日期。示例:(2010年6月20日)而不是(3年前)。

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


推荐答案

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

这篇关于Moment.js仅在上周内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆