【问题标题】:How to convert hours to days hours and minutes?如何将小时转换为天小时和分钟?
【发布时间】:2018-04-29 20:02:20
【问题描述】:

我有以小时为单位的数字,我需要将其转换为几天的小时和分钟。

例如我有这个小时数33.22

我需要将它转换为天小时和分钟。

结果应该是01.09:13:12(即一天 9 小时 13 分 12 秒)。

目前我这样做:

value = 33.22

var x = new Date(value * 3600000); //replace with TotalMilliSeconds in your case
var time = x.toUTCString().split(' ')[4]; //slit by space character

return time.split(':')[0] + ":" + time.split(':')[1];

但是我弄错的结果我认为我应该避免使用 Date 对象。

如果我有小时数,我如何获得天小时和分钟?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    我会编写一个函数convert,它将您的输入(小时)作为参数,将其转换为秒,计算天、小时和分钟,最后返回格式化字符串:

    const convert = (a) => {
      let secs = a * 3600;  // get total amount of seconds from your input
      const days = Math.floor(secs / (3600 * 24));
      secs  -= days * 3600 * 24;
      const hours   = Math.floor(secs / 3600);
      secs  -= hours * 3600;
      const mins = Math.floor(secs / 60);
      secs  -= mins * 60;
      console.log(`${days.toString().padStart(2,'0')}.${hours.toString().padStart(2,'0')}:${mins.toString().padStart(2,'0')}:${secs.toString().padStart(2,'0')}`);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      • 2019-02-22
      • 1970-01-01
      • 2014-12-01
      相关资源
      最近更新 更多