-1

I have two iso date and I want to calculate the day difference between two iso dates.I am unable to get the proper day difference.I want to calculate days.

date1=2020-08-14T09:10:49Z
 date2=2019-08-06T09:10:49Z 
 daydifference=date1-date2;
  • 1
    Does this answer your question? [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – holydragon Aug 14 '20 at 07:28
  • Does this answer your question? [Calculate difference between 2 dates in ISO format in JavaScript/TS](https://stackoverflow.com/questions/51882577/calculate-difference-between-2-dates-in-iso-format-in-javascript-ts) – Dokksen Aug 14 '20 at 07:33

1 Answers1

2

You could achieve this by calculate the difference in millisecond between two date then divide by 1 day (also in millisecond)

Below snippet could help you

const date1 = '2020-08-14T09:10:49Z'
const date2 = '2019-08-06T09:10:49Z'

const DAY_UNIT_IN_MILLISECONDS = 24 * 3600 * 1000

const diffInMilliseconds = new Date(date1).getTime() - new Date(date2).getTime()
const diffInDays = diffInMilliseconds / DAY_UNIT_IN_MILLISECONDS

console.log(diffInDays, 'day(s)')
hgb123
  • 13,869
  • 3
  • 20
  • 38