Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 691
William Poussier Avatar asked Jun 05 '15 13:06

William Poussier


People also ask

How do you check if a date is in between two dates MomentJS?

js. We can use the isBetween method to check if a date is between 2 dates. We create the compareDate moment object that we want to check if it's between startDate and endDate .

How can I check yesterday's date in moment?

To get yesterday's date with moment. js and JavaScript, we call the subtract method. const yesterday = moment().

How do you compare dates only in moment?

We can use the isAfter method to check if one date is after another. We create a moment object with a date string. Then we call isAfter on it with another date string and the unit to compare. Therefore isAfter is false since we compare the year only.

How do you find the day of the week from the moment?

This method will get/set the day of the week. It takes input from 0-6, where 0 is for Sunday and 6 as Saturday. If the value is greater than the range, it will fall in the next week.


1 Answers

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.

like image 63
acdcjunior Avatar answered Sep 20 '22 05:09

acdcjunior