0

I am trying to get nr of days between two dates. But using this code found in several places, I get illogical numbers of days for certain dates. Why is this happening and how can I get expected nr of days?

const oneDay = 24 * 60 * 60 * 1000

getDays = (d2, d1) => {
  let days = Math.round(Math.abs((d2 - d1) / oneDay))
  return days
}

const d1 = new Date(1918, 4, 13)

let d2 = new Date(2020, 4, 30)
console.log(getDays(d2, d1)) //Ok, Expected: 37273, Actual: 37273

d2 = new Date(2020, 5, 1)
console.log(getDays(d2, d1)) //Nok, Expected: 37274, Actual: 37275

Happens even if const d1 = new Date(2020, 4, 30)

Suresh Subedi
  • 660
  • 2
  • 10
  • 25

0 Answers0