-9

Say I have two dates: 1/24/2020 11:55:00 PM and 1/25/2020 01:00:00 AM. Real difference between them is about 2 hours but the day has changed. I want the difference in days which would be 1.

I have tried https://stackoverflow.com/a/52054494/6288172 but it doesn't give me the right answer.

ParmuTownley
  • 957
  • 2
  • 14
  • 34

1 Answers1

1
// your code goes here
var time = new Date(2020,0,25,23,23,23,2);
var date = new Date(time.toDateString()); // Sat Jan 25 2020 00:00:00 GMT+0000 (UTC)

var time1 = new Date(2020,0,26,2,2,2,2);
var date1 = new Date(time1.toDateString()); // Sun Jan 26 2020 00:00:00 GMT+0000 (UTC)

var diff = Math.round(Math.abs((date - date1)/(1000 * 60 * 60 * 24)));

console.log(diff); //1
Mike Reddington
  • 192
  • 2
  • 15