JavaScript seconds to time string with format hh:mm:ss
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 converting to x hours and x minutes...
View ArticleAnswer by Ash Burlaczenko for JavaScript seconds to time string with format...
A Google search turned up this result:function secondsToTime(secs){ secs = Math.round(secs); var hours = Math.floor(secs / (60 * 60)); var divisor_for_minutes = secs % (60 * 60); var minutes =...
View ArticleAnswer by powtac for JavaScript seconds to time string with format hh:mm:ss
String.prototype.toHHMMSS = function () { var sec_num = parseInt(this, 10); // don't forget the second param var hours = Math.floor(sec_num / 3600); var minutes = Math.floor((sec_num - (hours * 3600))...
View ArticleAnswer by Jellicle for JavaScript seconds to time string with format hh:mm:ss
I recommend ordinary javascript, using the Date object. (For a shorter solution, using toTimeString, see the second code snippet.)var seconds = 9999;// multiply by 1000 because Date() requires...
View ArticleAnswer by jottos for JavaScript seconds to time string with format hh:mm:ss
Variation on a theme. Handles single digit seconds a little differentlyseconds2time(0) -> "0s"seconds2time(59) -> "59s"seconds2time(60) -> "1:00"seconds2time(1000) ->...
View ArticleAnswer by Ninjakannon for JavaScript seconds to time string with format hh:mm:ss
A regular expression can be used to match the time substring in the string returned from the toString() method of the Date object, which is formatted as follows: "Thu Jul 05 2012 02:45:12 GMT+0100 (GMT...
View ArticleAnswer by Raj for JavaScript seconds to time string with format hh:mm:ss
To get the time part in the format hh:MM:ss, you can use this regular expression:(This was mentioned above in same post by someone, thanks for that.) var myDate = new...
View ArticleAnswer by Serge K. for JavaScript seconds to time string with format hh:mm:ss
I like the first answer.There some optimisations:source data is a Number. additional calculations is not needed.much excess computingResult code:Number.prototype.toHHMMSS = function () { var seconds =...
View ArticleAnswer by M Katz for JavaScript seconds to time string with format hh:mm:ss
Here is yet another version, which handles days also:function FormatSecondsAsDurationString( seconds ){ var s = ""; var days = Math.floor( ( seconds / 3600 ) / 24 ); if ( days >= 1 ) { s +=...
View ArticleAnswer by dt192 for JavaScript seconds to time string with format hh:mm:ss
This is how i did itfunction timeFromSecs(seconds){ return( Math.floor(seconds/86400)+'d :'+ Math.floor(((seconds/86400)%1)*24)+'h : '+ Math.floor(((seconds/3600)%1)*60)+'m : '+...
View ArticleAnswer by Pradeep for JavaScript seconds to time string with format hh:mm:ss
Using the amazing moment.js library:function humanizeDuration(input, units ) { // units is a string with possible values of y, M, w, d, h, m, s, ms var duration = moment().startOf('day').add(units,...
View ArticleAnswer by Artem Kyba for JavaScript seconds to time string with format hh:mm:ss
new Date().toString().split("")[4];result 15:08:03
View ArticleAnswer by Betamos for JavaScript seconds to time string with format hh:mm:ss
I dislike adding properties to standard datatypes in JavaScript, so I would recommend something like this:/** * Format a duration in seconds to a human readable format using the notion * "h+:mm:ss",...
View ArticleAnswer by Michael D. Moradzadeh for JavaScript seconds to time string with...
I'd upvote artem's answer, but I am a new poster. I did expand on his solution, though not what the OP asked for as follows t=(new Date()).toString().split(""); timestring =...
View ArticleAnswer by Andy Wu for JavaScript seconds to time string with format hh:mm:ss
s2t=function (t){ return parseInt(t/86400)+'d '+(new Date(t%86400*1000)).toUTCString().replace(/.*(\d{2}):(\d{2}):(\d{2}).*/, "$1h $2m $3s");}s2t(123456);result:1d 10h 17m 36s
View ArticleAnswer by webinista for JavaScript seconds to time string with format hh:mm:ss
If you know the number of seconds you have, this will work. It also uses the native Date() object.function formattime(numberofseconds){ var zero = '0', hours, minutes, seconds, time; time = new Date(0,...
View ArticleAnswer by Rutger van Baren for JavaScript seconds to time string with format...
I think performance wise this is by far the fastest:var t = 34236; // your secondsvar time = ('0'+Math.floor(t/3600) % 24).slice(-2)+':'+('0'+Math.floor(t/60)%60).slice(-2)+':'+('0'+ t %...
View ArticleAnswer by rookie1024 for JavaScript seconds to time string with format hh:mm:ss
Here's how I did it. It seems to work fairly well, and it's extremely compact. (It uses a lot of ternary operators, though)function formatTime(seconds) { var hh = Math.floor(seconds / 3600), mm =...
View ArticleAnswer by mashi for JavaScript seconds to time string with format hh:mm:ss
function toHHMMSS(seconds) { var h, m, s, result=''; // HOURs h = Math.floor(seconds/3600); seconds -= h*3600; if(h){ result = h<10 ? '0'+h+':' : h+':'; } // MINUTEs m = Math.floor(seconds/60);...
View ArticleAnswer by Harish Ambady for JavaScript seconds to time string with format...
You can manage to do this without any external JS library with the help of JS Date method like following:var date = new Date(0);date.setSeconds(45); // specify value for SECONDS herevar timeString =...
View Article