Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
How can I convert a timestamp to seconds in java script

I want to convert this:

00:07:57,685

to seconds. It should return 00*60 + 07*60 + 57,685 The problem is its format I did not manage to write an optimized function.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const input = "00:07:57,685";
const [hours, minutes, secondsRaw] = input.split(/:/g);
const seconds = secondsRaw.replace(",", ".");

let output = 0;
output += parseInt(hours) * 3600;
output += parseInt(minutes) * 60;
output += parseFloat(seconds);

console.log(`${output} seconds`);

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is a working sample :

function stringTimeToSecond (stringTime) {
    // convert from "," float notation to "." float notation
    // split your string to [h, m, s]
    // reverse to get [s, m, h] to be able to use indice on the reduce method
    const stringTimeArray = stringTime.replace(',','.').split(":").reverse();
    // 60^0 = 1 for seconds
    // 60^1 = 60 for minutes
    // 60^2 = 3600 for hours
    return stringTimeArray.reduce((timeInSecond, time, i) => {
         timeInSecond += time * Math.pow(60, i);
         return timeInSecond;
    }, 0);
}

Reduce method will iterate through your array and then return your accumulator "timeInSecond". The accumulator is initialized to 0 as the second argument of the reduce function.

about 4 years ago · Juan Pablo Isaza Relatório

0

I think this might work if i understood your question right:

let timestamp = "00:07:57,685"
let seconds = timestamp.split(":")[2].split(",")[0]
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda