moment.js 如何从秒数中获取准确的小时数?

moment.js How to get the exact hour from seconds?

我的项目中有一个计时器,我有以秒为单位的时间格式 90000 我想使用 moment.js 将秒转换为小时和分钟 (HH:mm),我想知道如何才能得到确切的时间,如果时间小于 86399 时刻按预期工作,但我的目标是得到这样的结果 25:00,这可能吗?

const secs = 90000;
const formatted = moment.utc(secs*1000).format('HH:mm');

document.write(formatted);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

试试这个

var seconds = 90000;
// multiply by 1000 because Date() requires miliseconds
var date = new Date(seconds * 1000);
var hh = date.getUTCHours();
var mm = date.getUTCMinutes();
var ss = date.getSeconds();
// If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
// if (hh > 12) {hh = hh % 12;}
// These lines ensure you have two-digits
if (hh < 10) {hh = "0"+hh;}
if (mm < 10) {mm = "0"+mm;}
if (ss < 10) {ss = "0"+ss;}
// This formats your string to HH:MM:SS
var t = hh+":"+mm+":"+ss;
document.write(t);
// result 1:00:00

因为 moment 使用 12/24 小时格式,所以不可能使用 moment。但是你可以使用基础数学

      const formatMMSS = (timeInSeconds) => {
        const time = (timeInSeconds/60/60).toFixed(2)
        const hh = time.split('.')[0]
        let mm = (parseFloat('0.'+time.split('.')[1])*60).toString().split('.')[0]
        if(mm.length == 1){
            mm = '0'+mm
           }
          return hh+':'+ mm;
        }
     console.log(formatMMSS(90000));