Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert '00: 30: 00 ' format to seconds?

I want to convert the format example '00: 30: 00' on seconds. This is my way. Is there a way to add these values in the 'template string'?

expected effect:

convert1000('00:30:00') --> 1800

convert1000('00:00:10') --> 10

convert1000('00:08:10') --> 490

convert1000('01:01:10') --> 3670

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

    if (hour !== "00") {
      return `${(hour* 3600)} + ${(minute * 60)} + ${(sec)}`;
    }

    if (minute !== "00") {
      return `${minute * 60} + ${sec} `;
    }

    return `${sec} `;
  }
like image 382
Umbro Avatar asked Dec 03 '22 18:12

Umbro


1 Answers

Use asSeconds from momentjs duration objects

moment.duration('00:30:00').asSeconds();
like image 110
Lucas Avatar answered Dec 26 '22 23:12

Lucas