Skip to content

Instantly share code, notes, and snippets.

@bhavyaw
Created March 16, 2017 09:33
Show Gist options
  • Save bhavyaw/a7f8663b28ea1f115021cad683e3ce30 to your computer and use it in GitHub Desktop.
Save bhavyaw/a7f8663b28ea1f115021cad683e3ce30 to your computer and use it in GitHub Desktop.
Minutes to HH:mm
formatMinutesToDuration(durationInMinutes){
let formattedDuration = ``;
if(durationInMinutes){
let derivedHours = round( (durationInMinutes / 60 ),3);
let hours = Math.trunc(derivedHours);
let derivedMinutes = Math.abs(derivedHours - Math.floor(derivedHours));
let minutes = Math.trunc(derivedMinutes * 60);
let hoursFormatted = hours ? ( (hours < 10) ? `0${hours}` : hours ) : "00";
let minuteFormatted = minutes ? ( (minutes < 10) ? `0${minutes}` : minutes ) : "00";
formattedDuration = `${hoursFormatted}:${minuteFormatted}`;
}
return formattedDuration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment