1

I am trying to get the days difference between two different dates like in these formats 2019-12-12T17:57:48.188-08:00 and other one is new Date() using the below formula

(Math.floor(new Date().getTime() - new Date(2019-12-12T17:57:48.188-08:00).getTime()) / (1000 * 3600 * 24)).toString()

but some how getting this value 0.15538604166666667

Could any one please suggest me on how to get exact difference in days between these two dates. many thanks.

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • 1
    Not all days are 24 hours long where daylight saving is observed, so the forumla used in the OP `(d1 - d2) / 8.64e7` is wrong where the range includes a daylight saving change. See [*this answer*](https://stackoverflow.com/a/11252167/257182) for more accurate results. – RobG Dec 14 '19 at 21:05

1 Answers1

3

Just try this code and make use of ceil() function.

let dated=(new Date().getTime() - new Date("2019-12-12T17:57:48.188-08:00").getTime()) / (1000 * 3600 * 24);
dated=Math.ceil(dated);

Output : 1

Prabhjot Singh Kainth
  • 1,831
  • 2
  • 18
  • 26
  • 1
    Code–only answers aren't that helpful. A good answer should explain why the OP has their issue and how your answer fixes it. – RobG Dec 14 '19 at 21:08