-1

I have the following date string: 2020-04-21T15:28:26.000Z

I would like to convert it into the amount of hours and minutes that have passed since that.

For example: 5:10

user2534723
  • 115
  • 1
  • 2
  • 11

2 Answers2

0

function getPassedTime(dateStr){
     const date = new Date(dateStr);
     const now = new Date();
     var diff = now.getTime() - date.getTime();
     var diffInMinutes = Math.round(diff / 60000);
     var hours = (diffInMinutes / 60);
     var passeHours = Math.floor(hours);
     var minutes = (hours - passeHours) * 60;
     var passedMinutes = Math.round(minutes);
     return passeHours+":"+passedMinutes;
}
console.log(getPassedTime('2020-04-21T15:28:26.000Z'))
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
0

function getDiffTime(time) {
  const old = new Date(time)
  const now = new Date();
  const diff = now - old;
  const msInHrs = 1000 * 60 * 60;
  const msInMn = 1000 * 60;
  const hrs = Math.floor(diff / msInHrs);
  const mn =  Math.floor((diff % (hrs * msInHrs)) / msInMn);
  return `${hrs}:${mn}`;
}

console.log(getDiffTime('2020-04-21T15:28:26.000Z'));
Cedric Cholley
  • 1,956
  • 2
  • 9
  • 15
  • This doesn't pad the minutes to 2 digits. You could also combine some of the steps to reduce the code, e.g. `let diff = Date.now() - Date.parse(time); let hrs = diff/3.6e6|0; let mins = (diff%3.6e6)/6e4|0`. :-) That should also be more efficient as it doesn't create any Date objects. – RobG Apr 21 '20 at 22:31