简体   繁体   English

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

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

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)?但是,我如何检查日期是一周前还是更早(例如两周前)?

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 demo with more tests, so you can tweak for your exact case, if needed.检查带有更多测试JSFiddle 演示,以便您可以根据需要调整您的确切情况。

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

The diff function 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演示

Simplest Solution for Today :今天最简单的解决方案:

const result = moment(date).isSame(moment(), "day")

Simplest Solution for Yesterday :昨天最简单的解决方案:

const result = moment(date).isSame(moment().subtract(1, 'day'), "day")

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/

Moment already contains some logic of this nature时刻已经包含了一些这种性质的逻辑

You may want to look at their source code for the humanize code https://github.com/moment/moment/blob/ed1fc742b179708d0a987a639979178616309f93/src/lib/duration/humanize.js您可能想查看他们的humanize代码的humanize代码https://github.com/moment/moment/blob/ed1fc742b179708d0a987a639979178616309f93/src/lib/duration/humanize.js

and the fromNow logicfromNow逻辑

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.对于“几天前”还可以的“帖子”日期似乎有意义,但我正在跟踪邮件中的包裹并说“您的包裹几天前发货”还不够好。

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

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