-3

I have a question about a method to calculate total days by javascript. Example: - startDate = 20170916 and closeDate = 20171224 => 99 days

What libs can I use to write this method? Note that can't use moment.js libs and I'm a newbie of js

Alex Le
  • 93
  • 3
  • 13

1 Answers1

0

Why bother with a library for something this simple?

Just subtract startDate ms from closeDate ms and then divide by ms in a day:

var msInADay = 86400000,
  startDate = new Date("2017-09-16"),
  closeDate = new Date("2017-12-24"),
  daysBetween = (closeDate.getTime() - startDate.getTime()) / msInADay;
console.log(daysBetween + " days between");
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • Given the [number of duplicates](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript), you probably should have marked it as a duplicate. – RobG Jun 13 '18 at 09:33
  • @RobG I did, and if it gets enough votes it will close, but until then this answers the question. – Emil S. Jørgensen Jun 13 '18 at 10:01