Quantcast
Channel: JavaScript seconds to time string with format hh:mm:ss - Stack Overflow
Browsing latest articles
Browse All 49 View Live

Answer by Safwat Fathi for JavaScript seconds to time string with format...

I think this function will do the trick:const isoToDate = ( iso: string, options?: Intl.DateTimeFormatOptions) => { const date = new Date(iso) const time = date.toLocaleTimeString( [], options || {...

View Article


Answer by Chris for JavaScript seconds to time string with format hh:mm:ss

Here's a variation of @meiyang's excellent solution that I ended up using: function duration(seconds) { return [ format(seconds / 60 / 60), format(seconds / 60 % 60), format(seconds % 60) ].join(':');...

View Article


Answer by RyadPasha for JavaScript seconds to time string with format hh:mm:ss

function secToTime(seconds, separator) { return [ parseInt(seconds / 60 / 60), parseInt(seconds / 60 % 60), parseInt(seconds % 60) ].join(separator ? separator : ':') .replace(/\b(\d)\b/g,...

View Article

Answer by Mr. Polywhirl for JavaScript seconds to time string with format...

Here is an example of using Date.prototype.toLocaleTimeString(). I chose GB as the language, because the US shows a 24 instead of a 00 for the initial hour. Furthermore, I chose Etc/UTC as the time...

View Article

Answer by Petr Újezdský for JavaScript seconds to time string with format...

The most general answer to this isfunction hms(seconds) { return [3600, 60] .reduceRight( (p, b) => r => [Math.floor(r / b)].concat(p(r % b)), r => [r] )(seconds) .map(a =>...

View Article


Answer by Arjun Kava for JavaScript seconds to time string with format hh:mm:ss

Easiest way to do it.new Date(sec * 1000).toISOString().substr(11, 8)

View Article

Answer by Matt Kenefick for JavaScript seconds to time string with format...

This is one I wrote recently for MM:SS. It's not exact to the question, but it's a different one-liner format.const time = 60 * 2 + 35; // 2 minutes, 35 secondsconst str = (~~(time / 60)...

View Article

Answer by Big Sam for JavaScript seconds to time string with format hh:mm:ss

Here is a fairly simple solution that rounds to the nearest second!var returnElapsedTime = function(epoch) { //We are assuming that the epoch is in seconds var hours = epoch / 3600, minutes = (hours %...

View Article


Answer by Max Yari for JavaScript seconds to time string with format hh:mm:ss

I saw that everybody's posting their takes on the problem despite the fact that few top answers already include all the necessary info to tailor for the specific use case.And since I want to be hip as...

View Article


Answer by DataGreed for JavaScript seconds to time string with format hh:mm:ss

Here's a one-liner updated for 2019://your datevar someDate = new Date("Wed Jun 26 2019 09:38:02 GMT+0100") var result =...

View Article

Answer by Godwin Vinny Carole for JavaScript seconds to time string with...

Here is an es6 Version of it:export const parseTime = (time) => { // send time in seconds// eslint-disable-next-line let hours = parseInt(time / 60 / 60), mins = Math.abs(parseInt(time / 60) -...

View Article

Answer by Israel for JavaScript seconds to time string with format hh:mm:ss

secToHHMM(number: number) { debugger; let hours = Math.floor(number / 3600); let minutes = Math.floor((number - (hours * 3600)) / 60); let seconds = number - (hours * 3600) - (minutes * 60); let H, M,...

View Article

Answer by artnikpro for JavaScript seconds to time string with format hh:mm:ss

/** * Formats seconds (number) to H:i:s format. * 00:12:00 * * When "short" option is set to true, will return: * 0:50 * 2:00 * 12:00 * 1:00:24 * 10:00:00 */export default function formatTimeHIS...

View Article


Answer by JukkaP for JavaScript seconds to time string with format hh:mm:ss

const secondsToTime = (seconds, locale) => { const date = new Date(0); date.setHours(0, 0, seconds, 0); return date.toLocaleTimeString(locale);}console.log(secondsToTime(3610, "en"));where the...

View Article

Answer by Boris Yakubchik for JavaScript seconds to time string with format...

There's a new method for strings on the block: padStartconst str = '5';str.padStart(2, '0'); // 05Here is a sample use case: YouTube durations in 4 lines of JavaScript

View Article


Answer by Rakesh for JavaScript seconds to time string with format hh:mm:ss

function secondsToTime(secs){ var hours = Math.floor(secs / (60 * 60)); var divisor_for_minutes = secs % (60 * 60); var minutes = Math.floor(divisor_for_minutes / 60); var divisor_for_seconds =...

View Article

Answer by Oleksiy Kachynskyy for JavaScript seconds to time string with...

You can use Momement.js with moment-duration-format plugin:var seconds = 3820;var duration = moment.duration(seconds, 'seconds');var formatted = duration.format("hh:mm:ss");console.log(formatted); //...

View Article


Answer by meiyang for JavaScript seconds to time string with format hh:mm:ss

function formatTime(seconds) { return [ parseInt(seconds / 60 / 60), parseInt(seconds / 60 % 60), parseInt(seconds % 60) ] .join(":") .replace(/\b(\d)\b/g, "0$1")}

View Article

Answer by John Slegers for JavaScript seconds to time string with format...

You can use the following function to convert time (in seconds) to HH:MM:SS format :var convertTime = function (input, separator) { var pad = function(input) {return input < 10 ? "0" + input :...

View Article

Answer by phpFreak for JavaScript seconds to time string with format hh:mm:ss

//secondsToTime(); var t = wachttijd_sec; // your seconds var hour = Math.floor(t/3600); if(hour < 10){ hour = '0'+hour; } var time = hour+':'+('0'+Math.floor(t/60)%60).slice(-2)+':'+('0'+ t %...

View Article

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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article



Answer 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

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer by Artem Kyba for JavaScript seconds to time string with format hh:mm:ss

new Date().toString().split("")[4];result 15:08:03

View Article

Answer 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 Article

Answer 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

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

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 Article

Browsing latest articles
Browse All 49 View Live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>