# javascript: smallest code to convert seconds to time [closed]

Given seconds like 11728, write the smallest javascript function that returns a string like 3hrs. 15min. 28sec.

signature textTime([int] t,[bool] z,[bool] m)

t is the time like 11728 and you can assume int
z is optional and true means drop zero times, so 3hrs. 0min. 28sec. becomes 3hrs. 28sec.
m is optional and true means show only up to minutes, so 3hrs. 16min. 48sec. is 3hrs. 16min.

For 1 hour, 1hrs. is fine (vs 1hr.)

Example:

textTime(11728) returns "3hrs. 0min. 28sec."
textTime(11728,true) returns "3hrs. 28sec."
textTime(11728,true,true) returns "3hrs."


Regex is fine, speed is inconsequential.

Shortest code wins.

• Your function parameters don't make any sense. Sep 12 '13 at 18:14
• What about days, weeks, months and years? Examples don't make a specification. Sep 13 '13 at 7:43
• this is not "date time", but a measure of hours and minutes and seconds. 1 day is not exactly 24hours and 0 secs, so a lot of things are measured in hours, which is the highest level measure of "real" time Sep 13 '13 at 19:10
• I apologize for using [brackets] to state the var type, which made it look like an array. maybe [int] t would have been better. Sep 13 '13 at 19:20
• jsfiddle.net/CA6GM/2 "Javascript 120" May 27 '14 at 12:22

# Javascript, 194 192

function T(a,c,e){d=60;s=["sec","min","hrs"];alert(t=[a,(0|a/d)*d,(0|a/d/d)*d*d].map(function(a,b,f){p=(a-(0|f[b+1]))/Math.pow(d,b);return e&&1>b?"":c&&!p?"":p+s[b]+". "}).reverse().join(""))}


Run it e.g. with

T(7215,true,false) //2hrs. 15sec.

Could probably be golfed a bit more
Edit Notes:
Added function name for the cost of 1 character* Renamed the function to T. I'm assigning t in the function hence i was overwriting the function itself. Changed (a/(d*d)) -> (a/d/d)

• Excellent!, it works in all cases. I'm marking this as the winner because it's current standard javascript. Sep 13 '13 at 19:31
• t(3600, true) gives TypeError: string is not a function. Also, a/(d*d)a/d/d and a-(0|f[b+1])0|a-f[b+1].
– Ry-
Sep 13 '13 at 23:32
• @minitech Thanks! I don't know how i missed the d/d. 1. Only the second time, called. I was overriding t 2.Changed it. 3. This is not quite correct. I'm needing the first one. as I'm accessing f[b+1] which can be undefined when exceeding the arrays range.2-(0|undefined /*0*/) //2 wheras 0|(2-undefined /*NaN*/) //0 Sep 14 '13 at 16:14
• Oh, in that case, try ~~f[b+1].
– Ry-
Sep 14 '13 at 19:08

155 147 characters.

textTime=(t,z,m,x)=>(x=[t/3600,t/60%60,t%60].map((t,i)=>(!z||t|0)&&~~t+["hrs","min","sec"][i]+"."||0).slice(0,2+!m),(z?x.filter(x=>x):x).join(" "))


If the parameters made a little more sense, yes, it would be fewer.

Non-ES6, changing the name and using globals like @C5H8NNaO4 did, at 183 characters:

function t(t,z,m){return(x=[t/3600,t/60%60,t%60].map(function(t,i){return(!z||t|0)&&~~t+["hrs","min","sec"][i]+"."||0}).slice(0,2+!m),(z?x.filter(function(x){return x}):x).join(" "))}

• How do i run it ? Sep 13 '13 at 12:45
• @C5H8NNaO4: The latest Firefox.
– Ry-
Sep 13 '13 at 14:19
• those are like c# lamdas right? are they (will they be) standard javascript? Sep 13 '13 at 19:13
• @AwokeKnowing: Yes, they’re part of ES6, the next/“current” standard. You can already use them in Firefox. In other browsers, it’s function t(t,z,m,x){return x=(m?[t/60,t%60]:[t/3600,t/60%60,t%60]).map(function(t,i){return(!z||t|0)&&~~t+["sec","min","hrs"][2-i-~~m]+"."||0}),(z?x.filter(function(x){return x;}):x).join(" ")} (193).
– Ry-
Sep 13 '13 at 20:53
• I got SyntaxError: Unexpected token ILLEGAL when I just pasted this version into chrome (the console) Sep 13 '13 at 23:22

# 165 chars

function x(T, Z, M){
f=Math.round;h=T/3600;m=h%1*60;s=m%1*60;
r=f(h)+'hrs '+f(m)+'min '+f(s)+'sec'
Z?r=r.replace(/0\w+/g,''):0
M?r=r.replace(/\d+sec/,''):0
return r}


I just realized that this solution doesn't work for T<60, so I added a few characters and that fixed it (see below):

## 167 chars

function x(T, Z, M){
f=Math.round;h=T/3600;m=h%1*60;s=m%1*60;
r=f(h)+'hrs '+f(~~m)+'min '+f(s)+'sec'
Z?r=r.replace(/0\w+/g,''):0
M?r=r.replace(/\d+sec/,''):0
return r}


If you don't care about getting the rounding quite right:

## 149 chars

function x(T, Z, M){
h=T/3600;m=h%1*60;s=m%1*60;
r=~~h+'hrs '+~~m+'min '+~~s+'sec'
Z?r=r.replace(/0\w+/g,''):0
M?r=r.replace(/\d+sec/,''):0
return r}

• Dat modulus though.. amazing! Nov 1 '15 at 12:26