1

I have date string 2022-01-03T00:00:00.000Z, i want to find the difference between today and this day in days. I tried this but not working as expected.

   var d1 = Date.now();
   var d2 = new Date('2020-01-03T00:00:00.000Z').getTime();
   console.log(d1);
   console.log(d2);


   var hours = hoursDiff(d1, d2);

   var daysDiff = Math.floor( hours / 24 );
   console.log(daysDiff);



  function hoursDiff(d1, d2) {
     var minutes = minutesDiff(d1, d2);
     var diff = Math.floor( minutes / 60 );
     return diff;
  }

 function minutesDiff(d1, d2) {
   var seconds = secondsDiff(d1, d2);
   var diff = Math.floor( seconds / 60 );
   return diff;
 }

  function secondsDiff(d1, d2) {
     var millisecondDiff = d2 - d1;
     var diff = Math.floor( ( d2 - d1) / 1000 );
     return diff;
   }
ASimla
  • 33
  • 3
  • "In this case the date difference is 13 days" - I'm not sure I understand what you mean. The date you have is in January of 2020 which is a while ago now. – Wander Nauta Apr 28 '21 at 11:52
  • 3
    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) – ValeriF21 Apr 28 '21 at 11:57

1 Answers1

1

See comments in snippet:

var d = new Date();

// build a date with 0 hours, minutes, seconds
var d1 = new Date(d.getFullYear() + '-' + ("0" + d.getMonth()).slice(-2) + '-' + d.getDate() + 'T00:00:00.000Z').getTime();

// date to compare
var d2 = new Date('2022-04-27T00:00:00.000Z').getTime();

// to seconds / to minutes / to hours/ to days
console.log((d1 - d2) / 1000 / 60 / 60 / 24);
tom
  • 9,550
  • 6
  • 30
  • 49