title: How to check if two dates are more than 7 days apart using moment.js
body: <p>I'm trying to determine whether there are more than 7 days between two dates using moment.js.</p>
  
  <p>code:</p>
  
  <pre><code>var start = moment(self.StartDate(), "DD/MM/YYYY");
  var end = moment(self.EndDate(), "DD/MM/YYYY");
  
  console.log(start);
  console.log(end);
  console.log(moment.duration(end.diff(start)).asDays());
  
  if (moment.duration(end.diff(start)).asDays() &gt; 7) {
      alertify.alert("Error", "Only a maximum of 7 days can be investigated.");
      return;
  }
  </code></pre>
  
  <p>This works if the two dates are within the same month. However, if the dates cross over between 2 months the <code>duration</code> returns a negative value.</p>
  
  <p>Example results:</p>
  
  <p><a href="https://i.stack.imgur.com/b2EcU.png" rel="noreferrer"><img src="https://i.stack.imgur.com/b2EcU.png" alt="enter image description here"></a></p>