优化后的代码如下:

function getaa(sNum){
    const d = Math.floor(sNum / 86400)
    const h = Math.floor((sNum % 86400) / 3600)
    const m = Math.floor((sNum % 3600) / 60)
    const s = sNum % 60
    return `${d}-${h}-${m}-${s}`
}

优化的地方如下:

  1. 使用 Math.floor() 替换 parseInt()Math.floor() 更快,且不会将非数字字符转化为数字。
  2. 使用 % 运算符替代乘法和除法运算,更高效。
JavaScript 秒数转换为天时分秒 - 高效优化代码

原文地址: https://www.cveoy.top/t/topic/orYu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录