## Javascript seconds to minutes and seconds

how to convert hours into minutes in javascript

javascript convert seconds to days, hours minutes

convert seconds to hours minutes/seconds angular 2

convert minutes to days hours minutes javascript

js seconds to timestamp

typescript convert seconds to hours minutes seconds

how to calculate minutes in javascript

This is a common problem but I'm not sure how to solve it. The code below works fine.

var mind = time % (60 * 60); var minutes = Math.floor(mind / 60); var secd = mind % 60; var seconds = Math.ceil(secd);

However, when I get to 1 hour or 3600 seconds it returns 0 minutes and 0 seconds. How can I avoid this so it returns all the minutes?

Thanks

To get the number of full minutes, divide the number of total seconds by 60 (60 seconds/minute):

var minutes = Math.floor(time / 60);

And to get the remaining seconds, multiply the full minutes with 60 and subtract from the total seconds:

var seconds = time - minutes * 60;

Now if you also want to get the full hours too, divide the number of total seconds by 3600 (60 minutes/hour · 60 seconds/minute) first, then calculate the remaining seconds:

var hours = Math.floor(time / 3600); time = time - hours * 3600;

Then you calculate the full minutes and remaining seconds.

Bonus:

Use the following code to pretty-print the time (suggested by Dru)

function str_pad_left(string,pad,length) { return (new Array(length+1).join(pad)+string).slice(-length); } var finalTime = str_pad_left(minutes,'0',2)+':'+str_pad_left(seconds,'0',2);

**How to convert seconds to minutes and hours with vanilla JS,** Working with time in JavaScript can be tricky. Today, I'm going to show you how to convert seconds in minutes and hours with vanilla JS. So you can see that it is a very simple and easy way to convert seconds into HH-MM-SS formatted time. Here in this example code, I have specified the seconds as 4995 and it returns the 1 as the hour, 23 as minutes and 15 as seconds. It returns the output 01:23:15. You can also try the above code and test it.

Another fancy solution:

function fancyTimeFormat(time) { // Hours, minutes and seconds var hrs = ~~(time / 3600); var mins = ~~((time % 3600) / 60); var secs = ~~time % 60; // Output like "1:01" or "4:03:59" or "123:03:59" var ret = ""; if (hrs > 0) { ret += "" + hrs + ":" + (mins < 10 ? "0" : ""); } ret += "" + mins + ":" + (secs < 10 ? "0" : ""); ret += "" + secs; return ret; }

`~~`

is a shorthand for `Math.floor`

, see this link for more info

**Javascript - Seconds to Time (hh:mm:ss,ms),** time = parseFloat(timeInSeconds).toFixed(3),. hours = Math.floor(time / 60 / 60),. minutes = Math.floor(time / 60) % 60,. seconds = Math.floor(time - minutes * 60),. In this javascript convert time tutorial. we will explain, In JavaScript how to convert hours to minutes, seconds and millisecond, how to convert minutes to seconds and milliseconds, how to convert seconds to milliseconds, how to convert date string to milliseconds in javascript

For people dropping in hoping for a quick simple and thus short solution to format seconds into `M:SS`

:

function fmtMSS(s){return(s-(s%=60))/60+(9<s?':':':0')+s}

done..
The function accepts *either* a `Number`

(preferred) *or* a `String`

(2 conversion 'penalties' which you can halve by prepending `+`

in the function call's argument for `s`

as in: `fmtMSS(+strSeconds)`

), representing positive integer seconds `s`

as argument.

Examples:

fmtMSS( 0 ); // 0:00 fmtMSS( '8'); // 0:08 fmtMSS( 9 ); // 0:09 fmtMSS( '10'); // 0:10 fmtMSS( 59 ); // 0:59 fmtMSS( +'60'); // 1:00 fmtMSS( 69 ); // 1:09 fmtMSS( 3599 ); // 59:59 fmtMSS('3600'); // 60:00 fmtMSS('3661'); // 61:01 fmtMSS( 7425 ); // 123:45

Breakdown:

function fmtMSS(s){ // accepts seconds as Number or String. Returns m:ss return( s - // take value s and subtract (will try to convert String to Number) ( s %= 60 ) // the new value of s, now holding the remainder of s divided by 60 // (will also try to convert String to Number) ) / 60 + ( // and divide the resulting Number by 60 // (can never result in a fractional value = no need for rounding) // to which we concatenate a String (converts the Number to String) // who's reference is chosen by the conditional operator: 9 < s // if seconds is larger than 9 ? ':' // then we don't need to prepend a zero : ':0' // else we do need to prepend a zero ) + s ; // and we add Number s to the string (converting it to String as well) }

Note: Negative range could be added by prepending `(0>s?(s=-s,'-'):'')+`

to the return expression (actually, `(0>s?(s=-s,'-'):0)+`

would work as well).

**How to convert JavaScript seconds to minutes and seconds?,** To convert JavaScript seconds to minutes and seconds, you can try to run the following code. <html> <head> <title>JavaScript Dates</title> The time in seconds is converted to a time in milliseconds by multiplying it by 1000. This is passed to the Date() constructor. The Date object created can then be used to access the hours, minutes and seconds as required for the format. The hours value is extracted from the date using the getUTCHours() method.

You can also use native Date object:

var date = new Date(null); date.setSeconds(timeInSeconds); // retrieve time ignoring the browser timezone - returns hh:mm:ss var utc = date.toUTCString(); // negative start index in substr does not work in IE 8 and earlier var time = utc.substr(utc.indexOf(':') - 2, 8) // retrieve each value individually - returns h:m:s var time = date.getUTCHours() + ':' + date.getUTCMinutes() + ':' + date.getUTCSeconds(); // does not work in IE8 and below - returns hh:mm:ss var time = date.toISOString().substr(11, 8); // not recommended - only if seconds number includes timezone difference var time = date.toTimeString().substr(0, 8);

Of course this solution works only for timeInSeconds less than 24 hours ;)

**Convert seconds to time value (Hours : Minutes : Seconds ),** Convert seconds to time value (Hours : Minutes : Seconds ) hours let minutes = Math.floor((sec - (hours * 3600)) / 60); // get minutes let seconds = sec - (hours * 3600) - (minutes * 60); Two easy ways to copy or clone an Array in Javascript. Your given seconds 123456 would be 1 days, 10 Hrs, 17 Minutes, 36 Seconds not 1 days, 4 Hrs, 40 Minutes, 45 Seconds

##### 2019 best variant

Format `hh:mm:ss`

console.log(display(60 * 60 * 2.5 + 25)) // 2.5 hours + 25 seconds function display (seconds) { const format = val => `0${Math.floor(val)}`.slice(-2) const hours = seconds / 3600 const minutes = (seconds % 3600) / 60 return [hours, minutes, seconds % 60].map(format).join(':') }

**Convert seconds to Hours Minutes Seconds in JavaScript ,** How to convert seconds to HH-MM-SS format so that it will show the hours, minute and seconds in hh:mm:ss format in JavaScript programming language? 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

**How to convert seconds to time string format hh:mm:ss using ,** How to call a function repeatedly every 5 seconds in JavaScript ? can then be used to access the hours, minutes and seconds as required for the format. On the second line, since there are 3600 seconds in 1 hour, we divide the total number of seconds by 3600 to get the total number of hours. We use parseInt to strip off any decimal. If totalSec was 12600 (3 and half hours), then parseInt( totalSec / 3600 ) would return 3, since we will have 3 full hours.

**javascript Convert Hours to Minutes,Minutes to Seconds,date to ,** In this javascript convert time tutorial. we will explain, In JavaScript how to convert hours to minutes, seconds and millisecond, how to convert The Date() object in javascript is not that smart really. If you just focus on adding seconds it seems to handle things smoothly but if you try to add X number of seconds then add X number of minute and hours, etc, to the same Date object you end up in trouble.

**keywords:seconds,** a very simple function to stringify any huge number of seconds. give it million seconds and it tells you how many days, hours, minutes and seconds in a pretty string. improved syntax – milliseconds resolution – both for node JS and browser I am creating a JS function that takes a time input in minutes only and divides that number by an inputted digit. I would like to know how we use Javascript to convert that result into minutes and seconds. For instance, 30 (1800 seconds) divided by 4 = 450 seconds.

##### Comments

- Thats because when time = 3600, 3600%3600 is always 0...so everything else will be 0 according to your calculation.
- It's a bit cleaner to get remaining seconds by doing 'var seconds = time % 60'.
- @Radio add leading zeros using
`function str_pad_left(string,pad,length){ return (new Array(length+1).join(pad)+string).slice(-length); } var finalTime = str_pad_left(minutes,'0',2)+':'+str_pad_left(seconds,'0',2);`

- This solution won't work for negative values of
`time`

. If you for instance input -1 seconds, you get -1minute and 59 seconds back... - What's the sense of having negative
`time`

? Logically a time difference is always positive - You can use modulus to get the number of seconds, it's more readable in my opinion.
`var seconds = time % 60`

- What's the meaning of
`~~`

? - It's a basic shorhand for
`Math.floor`

, see this link. - Its work fine..... :) You can round the value like this way hrs = hrs.toFixed(0); mins = mins.toFixed(0); secs = secs.toFixed(0);
- Thanks for this solution! I added
`time = math.round(time)`

to the first line to give me rounded seconds. - This is the more correct solution. As stated above, the solution with the most votes on this page displays 180 seconds as 2m60s.
- I'd suggest adding
`Math.floor(s)`

to the last line for cleaner results. Working great anyway, thanks!