1

I wrote my own JS function that splits a time value and returns it back formatted:

function formatTime(a) {
    var time = a.split(":");
    var hours = parseInt(time[0], 10);
    var minutes = parseInt(time[1], 10);
    var seconds = parseInt(time[2], 10);
    return hours + "h" + minutes + "m" + seconds + "s"
}

myTime = "00:38:51";
formatTime(myTime);

The result is 0h38m51s which is fine. However, now I want to remove the hour when it is set to 0. Can I achieve this without an if-statement?

Thank you nice folks.

fzzle
  • 1,466
  • 5
  • 23
  • 28
David Garcia
  • 3,056
  • 18
  • 55
  • 90

4 Answers4

2

Try this:

var hours = parseInt(time[0], 10);
hours = hours && (hours + 'h');
        :
return hours || '' + minutes + "m" + seconds + "s"

The trick is, that && returns its first operand if it is evaluated falsy (0 is a falsy value). Then in return || returns its second operand if the first operand is evaluated falsy.

Teemu
  • 22,918
  • 7
  • 53
  • 106
2

How about

return (hours ? (hours + "h") : "") + 
       (minutes ? (minutes + "m") : "") + 
       seconds + "s"

??

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • Could you please explain your answer in Pseudo-code... if "hours ?" check if there is a value then returns "(hours + "h") if true. else ":" return nothing? but then again 0 is still a value. – David Garcia Mar 22 '14 at 20:43
  • 1
    That's almost it. `test ? expr1 : expr2` checks whether `test` has a truthy value. If it does, the whole expression returns the result of evaluating `expr1`. If not, it returns the value of evaluating `expr2`. Since `0` is not a truthy value in JS (falsey ones are `false`, 0, -0, `null`, `undefined`, `NaN`, and `""`; everything else is truthy) this will return an empty string in precisely the case you care about, when `hours` is zero. – Scott Sauyet Mar 22 '14 at 21:03
1

I got rid of the 0h with a ternary operator:

t[i] = parseInt(t[i]) == 0 ? '' : parseInt(t[i]) + s[i + s.length - t.length]; 

Which is the same as:

if(parseInt(time[0], 10) == 0) { 
    time[0] = ''; 
} else { 
    time[0] = time[0] + 'h';
}

Hope it makes sense!

fzzle
  • 1,466
  • 5
  • 23
  • 28
  • 1
    Ternary operators seems very useful! to get rid if else etc ? what is their purpose really ( I will go and read upon also) – David Garcia Mar 22 '14 at 20:32
  • 1
    @J.D Ternary operators acts like an if / else statement. `(condition) ? (this returns if true) : (and this returns if false);` – fzzle Mar 22 '14 at 20:38
  • 1
    Actually it is better to avoid replacements of if/else by ternary, good explanation here http://stackoverflow.com/questions/6248920/expressions-in-javascript-ternary-operator-and-jslint – dmi3y Mar 22 '14 at 21:19
1

Wanna make it without if conditions, explicit or ternary operator? It's possible:

function formatTime(a) {

    var time = a.split(/:/),
        formatted = '', val, 
        units = ['h', 'm', 's'];

    while (time.length && (val = parseInt(time.pop(), 10))) {
        formatted = val + units.pop() + formatted;
    }

    return formatted;
}

formatTime('00:38:51'); // "38m51s"
formatTime('02:38:51'); // "2h38m51s"
dfsq
  • 191,768
  • 25
  • 236
  • 258