6

I'm trying to determine whether there are more than 7 days between two dates using moment.js.

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() > 7) {
    alertify.alert("Error", "Only a maximum of 7 days can be investigated.");
    return;
}

This works if the two dates are within the same month. However, if the dates cross over between 2 months the duration returns a negative value.

Example results:

enter image description here

Bhav
  • 1,584
  • 5
  • 24
  • 53
  • end.diff(start, 'days') // gives you diferent in integer – Arshpreet Wadehra Jun 25 '19 at 08:54
  • You can use Math.abs() for absolute value – DSCH Jun 25 '19 at 08:54
  • Docs for [diff()](https://momentjs.com/docs/#/displaying/difference/). Also from the docs, on diff: "If you want a floating point number, pass true as the third argument." – Kobe Jun 25 '19 at 08:55
  • I'd just convert to epoch timestamp, and find the num seconds between the two, then divide to get days... https://jsfiddle.net/JS69L/1/ – Lissy93 Jun 25 '19 at 08:56
  • Possible duplicate of [How to calculate number of days between two dates](https://stackoverflow.com/questions/9129928/how-to-calculate-number-of-days-between-two-dates) – Kobe Jun 25 '19 at 08:58

5 Answers5

10

Use diff method to check difference between two days and add days as second parameter to get difference in days.

var d1 = "2019-01-10";
var d2 = "2019-01-20";
var diff = moment(d2).diff(d1, 'days')
alert('difference :' + diff)

alert('is difference more than 7: ' + (diff > 7))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Neel Bhanushali
  • 755
  • 5
  • 9
3

You can do:

const d1 = moment([2019, 6, 30]);
const d2 = moment([2019, 6, 1]);
const diffDays = d1.diff(d2, 'days');

console.log(diffDays);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Yosvel Quintero Arguelles
  • 17,270
  • 4
  • 37
  • 42
0

With native JavaScript:

const dayInMiliseconds = 1000 * 60 * 60 * 24;
const self = {start: new Date(), end: new Date(new Date().setDate(new Date().getDate() + 7))}

console.log(Math.round((self.end - self.start) / dayInMiliseconds))
Ziv Ben-Or
  • 1,059
  • 6
  • 15
  • This really only works if the two dates are set to about the same time. E.g. the difference between 2019-06-23 06:00:00 and 2019-06-23 18:00:00 will resolve to 1 day, even though the difference is only 12 hours and both dates are on the same day. See [*How do I get the number of days between two dates in JavaScript?*](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – RobG Jun 25 '19 at 09:46
  • If you want a day to be counted only if passed 24 hours, use `Math.floor` insted `Math.round` – Ziv Ben-Or Jun 25 '19 at 09:51
-1

try this

end.diff(start, "days") > 7

docs: https://momentjs.com/docs/#/displaying/difference/

Ramil Amparo
  • 582
  • 1
  • 7
  • 21
-2

https://momentjs.com/docs/#/displaying/difference/

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b) // 86400000
  • 1
    Don't forget to divide an round, to get days from minutes, so that it answers the question. This is just copy pasted from the docs without using the OP's code, and with no explanation – Lissy93 Jun 25 '19 at 08:57
  • Ok, will improve my later comments, TY – Oleg Lozynskyi Jun 25 '19 at 09:00