0

I have a timer in my project, I have time format in seconds 90000 I want to transform seconds in hours and minutes (HH:mm) using moment.js, I wonder how I can get the exact time, if time is less than 86399 moment works as expected, but my aim is to get such a result 25:00, is this possible with moment?

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>
Nightcrawler
  • 943
  • 1
  • 11
  • 32

2 Answers2

2

Its not possible with moment since moment works with 12/24 hours formats. However you can use basic Math

      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));
Hussein Hn
  • 169
  • 1
  • 8
0

Try this

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