简体   繁体   English

与moment.js的相对时间,但有一天的粒度

[英]Relative time with moment.js but with one day granularity

I am using moment.js to display dates in a readable format. 我使用moment.js以可读格式显示日期。 What I want is to display them relatively to current time (1 day ago, 2 weeks ago and so on). 我想要的是相对于当前时间(1天前,2周前等)显示它们。

In documentation, I found that I have to use fromnow() , but I do not care about minutes, seconds and hours. 在文档中,我发现我必须使用fromnow() ,但我不关心分钟,秒和小时。 So instead of 6 minutes ago or 2 hours ago I want to show today. 因此,而不是6分钟前或2小时前,我想今天展示。 Further looking into documentation has not brought me anywhere. 进一步研究文档并没有把我带到任何地方。 Is there a way to do this? 有没有办法做到这一点?

Check if it's within 22 hours, and use "today" if that's the case - otherwise use from(): 检查它是否在22小时内,如果是这样则使用“今天” - 否则使用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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM