-1

Right now I want to know the difference between two DateTime

For Example

DateTime 1 = 2019-03-02T00:00:00

DateTime 2 = 2019-03-03T00:00:00

How do 1 check that it already past 24 hours or 1 day?

Community
  • 1
  • 1
CR97
  • 85
  • 1
  • 2
  • 13
  • "*How do 1 check that it already past 24 hours or 1 day?*" Which do you want? Not all days are 24 hours long where daylight saving is observed. Some might be 23 and some 25, or 23.5 and 24.5, and so on. So do you want one day or exactly 24 hours? – RobG Jun 03 '19 at 05:55

1 Answers1

6

You can simply do so by converting them to both JavaScript Date objects, before finding the difference between the 2 dates.

const date1 = new Date('2019-03-02T00:00:00');
const date2 = new Date('2019-03-03T00:00:00');
console.log(date2 - date1);
const hoursDifference = (date2 - date1) / (1000 * 60 * 60)
console.log(hoursDifference);
wentjun
  • 40,384
  • 10
  • 95
  • 107
  • You really shouldn't answer such an [*obvious duplicate*](https://stackoverflow.com/search?q=%5Bjavascript%5D+difference+between+dates). Also, in places where daylight saving is observed, a day can be less than 24 hours long. So clarification should have been sought whether the OP wants 1 day or exactly 24 hours. – RobG Jun 03 '19 at 05:53