Answer by Tom Esterez for JavaScript seconds to time string with format hh:mm:ss
Here's my take on it:function formatTime(ms: number) { const seconds = Math.floor(Math.abs(ms / 1000)) const h = Math.floor(seconds / 3600) const m = Math.floor((seconds % 3600) / 60) const s =...
View ArticleAnswer by user1683056 for JavaScript seconds to time string with format hh:mm:ss
This version of the accepted answer makes it a bit prettier if you are dealing with video lengths for example:1:37:40 (1 hour / 37 minutes / 40 seconds)1:00 (1 minute)2:20 (2 minutes and 20...
View ArticleAnswer by Vladimir for JavaScript seconds to time string with format hh:mm:ss
Here is my vision of solution. You can try my snippet below.function secToHHMM(sec) { var d = new Date(); d.setHours(0); d.setMinutes(0); d.setSeconds(0); d = new Date(d.getTime() + sec*1000); return...
View ArticleAnswer by Strong Bear for JavaScript seconds to time string with format hh:mm:ss
It's pretty easy,function toTimeString(seconds) { return (new Date(seconds * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];}
View ArticleAnswer by Peter Zehnder for JavaScript seconds to time string with format...
I'm personally prefer the leading unit (days, hours, minutes) without leading zeros. But seconds should always be leaded by minutes (0:13), this presentation is easily considered as 'duration', without...
View ArticleAnswer by nïkö for JavaScript seconds to time string with format hh:mm:ss
I liked Webjins answer the most, so i extended it to display days with a d suffix, made display conditional and included a s suffix on plain seconds:function sec2str(t){ var d = Math.floor(t/86400), h...
View ArticleAnswer by Mordred for JavaScript seconds to time string with format hh:mm:ss
I loved Powtac's answer, but I wanted to use it in angular.js, so I created a filter using his code. .filter('HHMMSS', ['$filter', function ($filter) { return function (input, decimals) { var sec_num =...
View ArticleAnswer by joan16v for JavaScript seconds to time string with format hh:mm:ss
Non-prototype version of toHHMMSS: function toHHMMSS(seconds) { var sec_num = parseInt(seconds); var hours = Math.floor(sec_num / 3600); var minutes = Math.floor((sec_num - (hours * 3600)) / 60); var...
View ArticleAnswer by Rodrigo Polo for JavaScript seconds to time string with format...
Milliseconds to duration, the simple way:// To have leading zero digits in strings.function pad(num, size) { var s = num +""; while (s.length < size) s = "0"+ s; return s;}// ms to...
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 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 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 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 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 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 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 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 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 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 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 Article