javascript Moment.js:获取与今天相关的日期(即“明天、今天、昨天等”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33987407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:05:58  来源:igfitidea点击:

Moment.js: Get day relevant to today (i.e. "Tomorrow, today, yesterday, etc")

javascriptjquerymomentjs

提问by Drakken Saer

How can I get Moment.js to return "Today" or other relevant terms? I cannot find anything in the docs that cover this.

如何让 Moment.js 返回“今天”或其他相关术语?我在涵盖此内容的文档中找不到任何内容。

回答by xersiee

You can also use calendar function:

您还可以使用日历功能:

moment().calendar(moment().add(1, 'day')); // "Yesterday at 9:14 PM"

http://momentjs.com/docs/#/displaying/calendar-time/

http://momentjs.com/docs/#/displaying/calendar-time/

回答by Meeh

I hope this will help you.

我希望这能帮到您。

moment('2013-02-04T10:35:24-08:00').fromNow(); // '3 years ago'
moment().subtract('days', 0).fromNow(); // 'a few seconds ago'
moment().subtract('days', 1).fromNow(); // 'a day ago'
moment().subtract('days', 7).fromNow(); // '7 days ago'

回答by Adam Gering

Today:

今天:

moment().startOf('day')

Tomorrow:

明天:

moment().startOf('day').add(1, 'day')

Yesterday:

昨天:

moment().startOf('day').subtract(1, 'day')