javascript - Relative time with moment.js but with one day granularity - Stack Overflow

I am using moment.js to display dates in a readable format. What I want is to display them relatively t

I am using moment.js to display dates in a readable format. What I want is to display them relatively to current time (1 day ago, 2 weeks ago and so on).

In documentation, I found that I have to use fromnow(), but I do not care about minutes, seconds and hours. So instead of 6 minutes ago or 2 hours ago I want to show today. Further looking into documentation has not brought me anywhere. Is there a way to do this?

I am using moment.js to display dates in a readable format. What I want is to display them relatively to current time (1 day ago, 2 weeks ago and so on).

In documentation, I found that I have to use fromnow(), but I do not care about minutes, seconds and hours. So instead of 6 minutes ago or 2 hours ago I want to show today. Further looking into documentation has not brought me anywhere. Is there a way to do this?

Share Improve this question edited May 15, 2014 at 19:32 sfletche 49.9k31 gold badges109 silver badges120 bronze badges asked May 15, 2014 at 19:27 Salvador DaliSalvador Dali 223k151 gold badges724 silver badges765 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Check if it's within 22 hours, and use "today" if that's the case - otherwise use from():

moment() < moment('2014-05-14 13:00:00').add('hours', 22) ? 'today' : moment('2014-05-14 13:00:00').from(moment());
"a day ago"

moment() < moment('2014-05-15 13:00:00').add('hours', 22) ? 'today' : moment('2014-05-15 13:00:00').from(moment());
"today"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744354263a4570154.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信