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

JavascriptDateMomentjs

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Solution 3 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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/

Solution 6 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWilliam PoussierView Question on Stackoverflow
Solution 1 - JavascriptacdcjuniorView Answer on Stackoverflow
Solution 2 - JavascriptGurpinderView Answer on Stackoverflow
Solution 3 - JavascriptAravindh SivalingamView Answer on Stackoverflow
Solution 4 - JavascriptwillView Answer on Stackoverflow
Solution 5 - JavascriptSimon_WeaverView Answer on Stackoverflow
Solution 6 - JavascriptSimon_WeaverView Answer on Stackoverflow