0

New to Javascript and learning about the Date object. I am working on an excercise that gives me two date objects, and I would like to get the number of days between them. To do this I will take the endate - startdate. How can I go about doing this since the dates are in this format below.

Edit1: I have looked at the previous asked questions but they don't answer how to format the dates I am asking.

startdate => 2019-04-01T04:00:00.000Z
endate => 2019-04-08T04:00:00.000Z
lets0code
  • 153
  • 3
  • 17
  • Possible duplicate of [How to calculate the number of days between two dates?](https://stackoverflow.com/questions/2627473/how-to-calculate-the-number-of-days-between-two-dates) – yqlim Apr 22 '19 at 01:10

1 Answers1

1

You can convert to Date because the strings follow the ISO 8601 format, subtract and finally calculate the days.

let startdate = "2019-04-01T04:00:00.000Z";
let endate = "2019-04-08T04:00:00.000Z";
let days = (new Date(endate) - new Date(startdate)) / 1000 / 60 / 60 / 24;

console.log(days);
Ele
  • 33,468
  • 7
  • 37
  • 75