Skip to content

Instantly share code, notes, and snippets.

@Code-Blender-7
Created January 2, 2023 03:20
Show Gist options
  • Save Code-Blender-7/dbb0a8982f3155490e503a7c6c7a9139 to your computer and use it in GitHub Desktop.
Save Code-Blender-7/dbb0a8982f3155490e503a7c6c7a9139 to your computer and use it in GitHub Desktop.
Converting minutes to hours, minutes using JavaScript
function timeConverter1(totalMinutes) {
const hours = Math.trunc(totalMinutes / 60);
const minutes = totalMinutes % 60;
return { hours, minutes };
}
// {hours: 3, minutes: 20}
console.log(timeConverter1(200));
function toHoursAndMinutes(totalMinutes) {
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
return { hours, minutes };
}
// {hours: 3, minutes: 20}
console.log(toHoursAndMinutes(200));
function timeConverter2(totalMinutes) {
const inHours = totalMinutes / 60;
const hours = totalMinutes >= 60 ? Math.trunc(inHours) : "";
const minute =
inHours % 1 === 0 ? "" : +(inHours.toFixed(2) + "").split(".")[1];
const minutes = +((60 / 100) * minute).toFixed();
return { hours, minutes };
}
// {hours: 3, minutes: 20}
console.log(timeConverter2(200));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment