JavaScript seconds to time string with format hh:mm:ss

前端 未结 30 1421
太阳男子
太阳男子 2020-11-22 07:19

I want to convert a duration of time, i.e., number of seconds to colon-separated time string (hh:mm:ss)

I found some useful answers here but they all talk about conv

30条回答
  •  抹茶落季
    2020-11-22 07:48

    secToHHMM(number: number) {
        debugger;
        let hours = Math.floor(number / 3600);
        let minutes = Math.floor((number - (hours * 3600)) / 60);
        let seconds = number - (hours * 3600) - (minutes * 60);
        let H, M, S;
        if (hours < 10) H = ("0" + hours);
        if (minutes < 10) M = ("0" + minutes);
        if (seconds < 10) S = ("0" + seconds);
        return (H || hours) + ':' + (M || minutes) + ':' + (S || seconds);
    }
    

提交回复
热议问题