Get Days Between Two Dates
Problem
You need to find how many seconds, minutes, hours, days, months or years have passed between two dates.
Solution
Use JavaScript’s Date function getTime(). Which provides how much time in milliseconds have passed since 01/01/1970:
Discussion
Using milliseconds makes the life easier to avoid overflow mistakes with Dates. So we first calculate how many milliseconds are in a day. Then, given two distinct dates, get the difference in milliseconds between two dates and then divide by how many milliseconds are in a day. It will return the days between two distinct dates.
If you’d like to calculate the hours between two date objects, you can do that by dividing the difference in milliseconds by the conversion of milliseconds to hours. The same goes for minutes and seconds.