javascript: smallest code to convert seconds to time

0

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.

AwokeKnowing

Posted 2013-09-12T17:18:08.367

Reputation: 201

Question was closed 2013-09-23T12:20:03.410

4Your function parameters don't make any sense. – Shmiddty – 2013-09-12T18:14:47.263

1What about days, weeks, months and years? Examples don't make a specification. – Johannes Kuhn – 2013-09-13T07:43:03.093

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 – AwokeKnowing – 2013-09-13T19:10:53.700

I apologize for using [brackets] to state the var type, which made it look like an array. maybe [int] t would have been better. – AwokeKnowing – 2013-09-13T19:20:37.567

http://jsfiddle.net/CA6GM/2/ "Javascript 120" – cocco – 2014-05-27T12:22:27.000

Answers

1

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)

C5H8NNaO4

Posted 2013-09-12T17:18:08.367

Reputation: 1 340

1Excellent!, it works in all cases. I'm marking this as the winner because it's current standard javascript. – AwokeKnowing – 2013-09-13T19:31:29.240

1t(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- – 2013-09-13T23:32:42.113

@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 – C5H8NNaO4 – 2013-09-14T16:14:46.323

Oh, in that case, try ~~f[b+1]. – Ry- – 2013-09-14T19:08:27.780

1

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(" "))}

Ry-

Posted 2013-09-12T17:18:08.367

Reputation: 5 283

How do i run it ? – C5H8NNaO4 – 2013-09-13T12:45:15.600

@C5H8NNaO4: The latest Firefox. – Ry- – 2013-09-13T14:19:55.367

those are like c# lamdas right? are they (will they be) standard javascript? – AwokeKnowing – 2013-09-13T19:13:22.053

@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- – 2013-09-13T20:53:22.077

I got SyntaxError: Unexpected token ILLEGAL when I just pasted this version into chrome (the console) – AwokeKnowing – 2013-09-13T23:22:27.310

@AwokeKnowing: Copying that adds some silly carriage returns or something. I’ll add it to the answer. – Ry- – 2013-09-13T23:30:14.527

t(3600,1,1) //"60min." shouldn't that be "1hrs."? – C5H8NNaO4 – 2013-09-14T18:03:03.447

@C5H8NNaO4: Ah, yes, there were more details added to the question. I originally interpreted “show up to minutes” as “show minutes and seconds”, not “show hours and minutes”. Give me a minute… – Ry- – 2013-09-14T18:26:45.497

1

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}

tristin

Posted 2013-09-12T17:18:08.367

Reputation: 189

Dat modulus though.. amazing! – NiCk Newman – 2015-11-01T12:26:21.283