Linked Questions

Popular Questions

Javascript function: Returning value after a split

Asked by At

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";

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.

Related Questions