Javascript moment.js - 测试日期是今天、昨天、一周或两周前

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30668373/
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-08-23 05:33:53  来源:igfitidea点击:

moment.js - test if a date is today, yesterday, within a week or two weeks ago

javascriptdatemomentjs

提问by William Poussier

I am trying with moment.js to know if a date is today, yesterday, 1 week ago, or older (2 weeks ago or more).

我正在尝试使用 moment.js 来了解日期是今天、昨天、1 周前还是更早(2 周前或更长时间)。

I already done that for the first two cases:

对于前两种情况,我已经这样做了:

var today = moment().startOf('day');
var yesterday = moment().subtract(1, 'days').startOf('day');

if (moment(localTime).isSame(today, 'd')) // today
    // do something
if (moment(localTime).isSame(yesterday, 'd')) // yesterday
    // do something

Is that correct?

那是对的吗?

However, how could I check if a date is a week ago, or older (eg. two weeks ago)?

但是,我如何检查日期是一周前还是更早(例如两周前)?

回答by acdcjunior

Here's something that can be useful:

这里有一些有用的东西:

var REFERENCE = moment("2015-06-05"); // fixed just for testing, use moment();
var TODAY = REFERENCE.clone().startOf('day');
var YESTERDAY = REFERENCE.clone().subtract(1, 'days').startOf('day');
var A_WEEK_OLD = REFERENCE.clone().subtract(7, 'days').startOf('day');

function isToday(momentDate) {
    return momentDate.isSame(TODAY, 'd');
}
function isYesterday(momentDate) {
    return momentDate.isSame(YESTERDAY, 'd');
}
function isWithinAWeek(momentDate) {
    return momentDate.isAfter(A_WEEK_OLD);
}
function isTwoWeeksOrMore(momentDate) {
    return !isWithinAWeek(momentDate);
}

console.log("is it today? ..................Should be true: "+isToday(moment("2015-06-05")));
console.log("is it yesterday? ..............Should be true: "+isYesterday(moment("2015-06-04")));
console.log("is it within a week? ..........Should be true: "+isWithinAWeek(moment("2015-06-03")));
console.log("is it within a week? ..........Should be false: "+isWithinAWeek(moment("2015-05-29")));
console.log("is it two weeks older or more? Should be false: "+isTwoWeeksOrMore(moment("2015-05-30")));
console.log("is it two weeks older or more? Should be true: "+isTwoWeeksOrMore(moment("2015-05-29")));

Check a JSFiddle demowith more tests, so you can tweak for your exact case, if needed.

检查带有更多测试JSFiddle 演示,以便您可以根据需要调整您的确切情况。

回答by Gurpinder

More precise answer as follows

更准确的答案如下

var today = moment();
var yesterday = moment().subtract(1, 'day');

var engagementDate = (Date to be compare);

if(moment(engagementDate).isSame(today, 'day'))
  console.log('Today');
else if(moment(engagementDate).isSame(yesterday, 'day'))
  console.log('Yesterday');

回答by will

The difffunction might be helpful for your case and, in general, to check the exact days since a date

DIFF功能可能会为你的情况是有帮助的,在一般情况下,由于时间段查看确切天

var sampleDaysAgo = moment().subtract(28, 'days'); //28 just for test
var today = moment();

console.log(today.diff(sampleDaysAgo, 'days')); // 28 

Demo

演示

回答by Simon_Weaver

From version 2.14 onwards you can customize the calendar()function

从 2.14 版本开始,您可以自定义该calendar()功能

moment().calendar(YOUR_DATE, {
  sameDay: function (now) {
    if (this.isBefore(now)) {
      return '[Will Happen Today]';
    } else {
      return '[Happened Today]';
    }
    /* ... */
  }
});

This is designed for 'calendar' type events.

这是为“日历”类型的事件设计的。

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

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

回答by Simon_Weaver

Moment already contains some logic of this nature

时刻已经包含了一些这种性质的逻辑

You may want to look at their source code for the humanizecode https://github.com/moment/moment/blob/ed1fc742b179708d0a987a639979178616309f93/src/lib/duration/humanize.js

您可能想查看他们的源humanize代码 https://github.com/moment/moment/blob/ed1fc742b179708d0a987a639979178616309f93/src/lib/duration/humanize.js

and the fromNowlogic

fromNow逻辑

https://github.com/moment/moment/blob/497f918515ae6ab900f008f19523b1e4ae5e2450/src/lib/moment/from.js

https://github.com/moment/moment/blob/497f918515ae6ab900f008f19523b1e4ae5e2450/src/lib/moment/from.js

I found this useful to see how they did it.

我发现这有助于了解他们是如何做到的。

The way you format the string will depend on your business, and to be honest the built in logic probably won't cut it most of the time. It seems to make sense for the date of a 'post' where 'a few days ago' is ok but I'm tracking packages in the mail and saying 'Your package shipped a few days ago' just isn't good enough.

格式化字符串的方式取决于您的业务,老实说,大多数情况下内置逻辑可能不会削减它。对于“几天前”还可以的“帖子”日期似乎有意义,但我正在跟踪邮件中的包裹并说“您的包裹几天前发货”还不够好。