Reputation: 639
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.
Upvotes: 1
Views: 150
Reputation: 792
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);
Source: Convert seconds to HH-MM-SS format in JavaScript
Upvotes: 0
Reputation: 4110
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);
Upvotes: 1
Reputation: 13600
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.
Upvotes: 1