javascriptdatemethods

Library to calculate date in Javascript


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


Solution

  • 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");