0

I want to convert time from the format. I want to get the following effect, for example: 03 s -> 3s; 01 h 04 m 05 s -> 1 h 4 m 5 s; 01h -> 1h; 01h 15 m 07 s -> 1h 15 m 7 s

I use library timelite / time.

 let time = 00:00:03
 let a = add(time);
 let b = str(a);

 convert = (time) => {
    const [hour, minute, sec] = time.split(':');

    if (hour !== "00") {
      return `${hour} h ${minute} m ${sec} s`;
    }

    if (minute !== "00") {
      return `${minute} m ${sec} s`;
    }

    return `${sec} s`;
  }

console.log(convert(b))  //output 03s
Umbro
  • 1,984
  • 12
  • 40
  • 99
  • @Heretic Monkey It isn't duplicate. My example: 02:02:02 --> my output 2:2:2. Yours post "Remove leading zeros from time format " delete only first 0 --> 2:02:02 – Umbro Jun 05 '19 at 18:45
  • @ScottMarcus Maybe [Remove/ truncate leading zeros by javascript/jquery](https://stackoverflow.com/q/8276451/215552) would be more appropriate. The OP could run that on each of `[hour, minute, sec]`. – Heretic Monkey Jun 05 '19 at 18:57
  • No clue helped me. I'm using `replace (/ ^ 0 + /, '')` on hours, minutes, sec. I remove zero. But if I have for example: 02 h 00 m 07s this is a defect 2h m 7s. The letter `m` remains – Umbro Jun 05 '19 at 19:57
  • If you are using momentjs, this may help you: https://momentjs.com/docs/#/displaying/format/ – Harshad Jun 05 '19 at 20:31

0 Answers0