moment.js - test if a date is today, yesterday, wi

2019-03-11 00:38发布

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

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)?

5条回答
对你真心纯属浪费
2楼-- · 2019-03-11 00:57

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.

查看更多
淡お忘
3楼-- · 2019-03-11 01:06

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

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

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

Demo

查看更多
可以哭但决不认输i
4楼-- · 2019-03-11 01:08

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

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/

查看更多
smile是对你的礼貌
5楼-- · 2019-03-11 01:09

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');
查看更多
虎瘦雄心在
6楼-- · 2019-03-11 01:12

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

and the fromNow logic

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.

查看更多
登录 后发表回答