Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert seconds to minutes and hours in javascript

Tags:

javascript

I have a count of seconds stored in variable seconds. I want to convert for example 1439 seconds to 23 minutes and 59 seconds. And if the time is greater than 1 hour (for example 9432 seconds), to 2 hours, 37 minutes and 12 seconds.

How can I achieve this?

I'm thinking of:

var sec, min, hour;  if(seconds<3600){     var a = Math.floor(seconds/60); //minutes     var b = seconds%60; //seconds      if (b!=1){         sec = "seconds";     }else{         sec = "second";     }      if(a!=1){         min = "minutes";     }else{         min = "minute";     }      $('span').text("You have played "+a+" "+min+" and "+b+" "+sec+"."); }else{         var a = Math.floor(seconds/3600); //hours     var x = seconds%3600;     var b = Math.floor(x/60); //minutes     var c = seconds%60; //seconds       if (c!=1){         sec = "seconds";     }else{         sec = "second";     }      if(b!=1){         min = "minutes";     }else{         min = "minute";     }      if(c!=1){         hour = "hours";     }else{         hour = "hour";     }      $('span').text("You have played "+a+" "+hour+", "+b+" "+min+" and "+c+" "+sec+"."); } 

But that's a lot of code, and it has to be calculated each second. How can I shrink this up?

like image 225
ALSD Minecraft Avatar asked May 08 '16 04:05

ALSD Minecraft


People also ask

How do you convert SEC to HH MM SS?

How to Convert Seconds to Hours. The time in hours is equal to the time in seconds divided by 3,600.

How do you convert days seconds to hours minutes and seconds?

int seconds = (totalSeconds % 60); int minutes = (totalSeconds % 3600) / 60; int hours = (totalSeconds % 86400) / 3600; int days = (totalSeconds % (86400 * 30)) / 86400; First line - We get the remainder of seconds when dividing by number of seconds in a minutes.


2 Answers

I think you would find this solution very helpful.

You modify the display format to fit your needs with something like this -

function secondsToHms(d) {     d = Number(d);     var h = Math.floor(d / 3600);     var m = Math.floor(d % 3600 / 60);     var s = Math.floor(d % 3600 % 60);      var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";     var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";     var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";     return hDisplay + mDisplay + sDisplay;  } 
like image 66
Wilson Lee Avatar answered Oct 07 '22 18:10

Wilson Lee


You can try this, i have used this successfully in the past You should be able to add the minutes and seconds on easily

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 = divisor_for_minutes % 60;     var seconds = Math.ceil(divisor_for_seconds);      var obj = {         "h": hours,         "m": minutes,         "s": seconds     };     return obj; } 

Fiddle

You can change the object to

var obj = {         "h": hours + " hours",         "m": minutes + " minutes",         "s": seconds + " seconds"     }; 
like image 35
R4nc1d Avatar answered Oct 07 '22 18:10

R4nc1d