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.

5
  • 1
    You could do this using a ternary operator, but I don't understand why you would want to avoid an if. Commented Mar 22, 2014 at 20:16
  • Another member made this http://jsfiddle.net/QJq7B/ but i decided to code my own, i am not sure how he is getting rid of the 0 value in hour Commented Mar 22, 2014 at 20:19
  • Didn't I just answer this? -> stackoverflow.com/questions/22580806/javascript-split-time Commented Mar 22, 2014 at 20:20
  • This is a different question @adeneo i ended up coding my own function Commented Mar 22, 2014 at 20:22
  • It seems like it's exactly the same, look at my deleted answer that was downvoted, and it does exactly what you're asking for ? Commented Mar 22, 2014 at 20:23

4 Answers 4

2

How about

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

??

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

3 Comments

hours + "h" is probably never going to be falsy.
Your edited code does not put "h" into the string when hours is non zero.
Exiting stuff. Ill go and read more about ternary operators! thanks for your input
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!

3 Comments

Ternary operators seems very useful! to get rid if else etc ? what is their purpose really ( I will go and read upon also)
@J.D Ternary operators acts like an if / else statement. (condition) ? (this returns if true) : (and this returns if false);
Actually it is better to avoid replacements of if/else by ternary, good explanation here stackoverflow.com/questions/6248920/…
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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.