ms、s、min、h单位换算代码可以再优化吗?

export const timeConvert = function (timeValue) {
  if (isNaN(timeValue)) {
    return '';
  }
  const baseUnitLabel = ['ms', 's', 'min', 'h'];
  const basicValue = [1,1000,1000*60,1000*60*60]
  const exp1 = Math.floor(timeValue / 1000);
  let exp2;
  if (exp1 < 1) {
    exp2 = 0
  } else if (exp1 < 60) {
    exp2 =  1
  } else if(exp1 < 60 * 60)  {
    exp2 = 2
  } else {
    exp2 = 3
  }
  let resValue = timeValue/basicValue[exp2]
  if (resValue.toString().length > resValue.toFixed(2).toString().length) {
    resValue = resValue.toFixed(2);
  }
  return `${resValue} ${baseUnitLabel[exp2]}`;
};

之后优化修改

export const timeConvert = function (timeValue) {
  if (isNaN(timeValue) || typeof timeValue != 'number') {
    return '';
  }
  const baseUnitLabel = ['ms', 's', 'min', 'h'];
  const baseUnitValue = [1,1000,1000*60,1000*60*60]
  const formatValue = Math.floor(timeValue / 1000);
  let resIndex;
  if (formatValue < 1) {
    resIndex = 0
  } else {
    const value = Math.floor(Math.log(formatValue) / Math.log(60)) + 1;
    resIndex = value > 3? 3: value
  }
  let resValue = timeValue / baseUnitValue[resIndex]
  if (resValue.toString().length > resValue.toFixed(2).length) {
    resValue = resValue.toFixed(2);
  }
  return `${resValue} ${baseUnitLabel[resIndex]}`;
};
阅读 1.9k
4 个回答
export const timeConvert = function (timeValue) {
    if (!(typeof timeValue === "number" && timeValue >= 0)) {
        return "";
    }

    const baseUnitLabel = ["ms", "s", "min", "h"];
    const basicValue = [1, 1000, 1000 * 60, 1000 * 60 * 60];

    let index = Math.max(basicValue.findLastIndex(basic => timeValue > basic), 0)
    
    return `${Number((timeValue / basicValue[index]).toFixed(2))} ${baseUnitLabel[index]}`;
};

简单的优化了一下

 export const timeConvert =  function (timeValue) {
    if (isNaN(timeValue) || typeof timeValue != 'number') {
      return '';
    }
    const baseUnitLabel = ['ms', 's', 'min', 'h'];
    const basicValue = [1, 1000, 1000 * 60, 1000 * 60 * 60]
    const exp1 = Math.floor(timeValue / 1000);
    let exp2 = 3;
    if (exp1 < 1) {
      exp2 = 0
    } else if (exp1 < 60) {
      exp2 = 1
    } else if (exp1 < 60 * 60) {
      exp2 = 2
    }
    let resValue = timeValue / basicValue[exp2]
    if (resValue.toString().length > resValue.toFixed(2).length) {
      resValue = Math.round((resValue + Number.EPSILON) * 100) / 100;
    }
    return `${resValue} ${baseUnitLabel[exp2]}`;
  };

image.png

本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。
export const timeConvert = function (timeValue) {
    if (!(typeof timeValue === "number" && timeValue >= 0)) {
        return "";
    }

    const baseUnitLabel = ["ms", "s", "min", "h"];
    const basicValue = [1, 1000, 1000 * 60, 1000 * 60 * 60];

    for (let i = 3; i > 0; i--) {
        const base = basicValue[i];
        if (timeValue > base) {
            return `${(timeValue / base).toFixed(2)} ${baseUnitLabel[i]}`;
        }
    }
    return `${timeValue} ${baseUnitLabel[0]}`;
};
const basicValue = [1,1000,1000*60,1000*60*60]

这个数组没有必要,完全可以计算出来

const timeConvert2 = function(timeValue) {
    if (isNaN(timeValue) || typeof timeValue != 'number') {
        return '';
    }
    const baseUnitLabel = ['ms', 's', 'min', 'h'];
    //[60**0,60**1,60**2]
    if (timeValue <= 1000) { 
        return `${timeValue} ${baseUnitLabel[0]}`;
    } else {
        let exp2 = Math.floor(Math.log(timeValue / 1000) / Math.log(60))
        if (exp2 < 1) {
            exp2 = 0
        }
        if (exp2 > 2) {
            exp2 = 2
        }

        let resValue = timeValue / (Math.pow(60, exp2) * 1000);
        if (resValue.toString().length > resValue.toFixed(2).length) {
            resValue = Math.round((resValue + Number.EPSILON) * 100) / 100;
        }
        return `${resValue} ${baseUnitLabel[exp2+1]}`;
    }
};
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏