-2

Hey how can you find the number of days difference in javascript between two dates with a "yyyymmdd" format?

For instance 20210206 - 20210825 will return 619, however these dates are actually only 200 days apart.

Also, I have to do with the dates as just integers. I don't have access to any special date functions.

Is there a math genius out there who knows what to do?

1 Answers1

0

Use String.substring to split the string into 3 parts (year, month, date) and pass them as parameters in the Date object, then subtract and divide by 86400000:

function convertToDate(s){
  return new Date(s.substring(0, 4) - 1, s.substring(4, 6) -1, s.substring(6));
}
const first = "20210206";
const second = "20210825";
const diff = Math.floor(Math.abs((convertToDate(first) - convertToDate(second)) / 86400000))
console.log(diff)
Spectric
  • 30,714
  • 6
  • 20
  • 43