JSFiddle - React, Tailwind, and code Playground

by fergal_doyle

JavaScript

//convert everything to minutes
//if less than 60, only mintues
//if greater than||eq 60, hours + min, div by 60 to get hour, mod to get min
//if greater than||eq 480, days. div by 480
function convert(v) {
  var out;

  //convert to mins
  v = (v * 60);

  if (v < 60) {
    out = Math.round(v) + " minutes";
  } else if (v >= 60 && v < 480) {
    out = (v / 60);
    if (v % 60) {
      out = String(out).replace(/\..*/, "") + " hour " + Math.round(v % 60) + " minutes"
    } else {
      out += " hour"
    }
    if(v > 119){out = out.replace("hour", "hours")}

  } else {
    out = (v / 480);
    if (v % 480) {
      out = out.toFixed(1)
    }
    out += " days";
  }

  return out;
}

console.log(convert("1.25"));