How can I convert seconds to (H hr mm min) format by Javascript? Example : 4 hr 30 min I found other solutions here, but they didn't help me.
            Asked
            
        
        
            Active
            
        
            Viewed 140 times
        
    1
            
            
        - 
                    Take a look at [this question](http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript) – sbgoran Aug 02 '13 at 11:23
- 
                    i don't want the current time; i want to use my custom second to convert it to H hr MM min format e.g. : 3 hr 22 min – iFarbod Aug 02 '13 at 12:17
- 
                    Date constructor accepts custom date parameter(s), you can construct dates like this: `new Date ( year, month, date, hour, minute, second )`. Look at accepted answer on link I give you (look at links in answer too), there you have detail answer regarding date manipulation in JS. – sbgoran Aug 02 '13 at 13:11
3 Answers
1
            hours is
(total_seconds / 60) / 60
minutes is
(total_seconds / 60) % 60
seconds is
(total_seconds % 60) % 60
where / is integer division (division that discards the remainder) and % is the modulo function.
 
    
    
        Justin L.
        
- 13,510
- 5
- 48
- 83
- 
                    There's no such thing called "integer division" in JavaScript. `1/3 = 0.3333.....`, not `0`. You have to use [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) to discard the fraction. – Rob W Aug 02 '13 at 11:15
- 
                    i'm referring to the mathematical operation; integer division in javascript can be implemented as `Math.foor(a/b)`. – Justin L. Aug 02 '13 at 11:17
1
            
            
        Use JavaScript's built-in Date function:
// Randomly selected number of seconds
var seconds = 23568;
// Pass it to the Date-constructor (year, month, day, hours, minutes, seconds)
var d = new Date(0, 0, 0, 0, 0, seconds);
// Get result as a "formatted" string, and show it.
var myString = d.getHours().toString() + ':' + d.getMinutes().toString() + ':' + d.getSeconds().toString();
alert(myString);
 
    
    
        UweB
        
- 4,080
- 2
- 16
- 28
0
            
            
        Below is the given code which will convert seconds into hh-mm-ss format:
var measuredTime = new Date(null);
measuredTime.setSeconds(4995); // specify value of SECONDS
var MHSTime = measuredTime.toISOString().substr(11, 8);
 
    
    
        Faruque Ahamed Mollick
        
- 775
- 13
- 15
