How to calculate the number of days between two dates?

udaya picture udaya · Apr 13, 2010 · Viewed 401.7k times · Source
  1. I am calculating the number of days between the 'from' and 'to' date. For example, if the from date is 13/04/2010 and the to date is 15/04/2010 the result should be

  2. How do I get the result using JavaScript?

Answer

MaxVT picture MaxVT · Apr 13, 2010
const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
const firstDate = new Date(2008, 1, 12);
const secondDate = new Date(2008, 1, 22);

const diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay));