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

169
Visualizações
How to subtract 10 seconds from a specific given timestamp JS

Here is the time:

01:02:25,369

I want to subtract 10 seconds from it, so the return value should be:

01:02:15,369

I know I have to write a function to do so, but things get difficult when seconds are <10 so you have to subtract minutes and hours as well.

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

0

Turn it into a Date, then subtract 10000 (don't reinvent the wheel)

about 4 years ago · Juan Pablo Isaza Relatório

0

You could just convert it to milliseconds and then add or subtract however much you want. And then make a render function to convert it back into a string if you need it.

function getTotalMS(timeString) {
    const [hms, ms] = timeString.split(',');
    const [h, m, s] = hms.split(':');

    return parseInt(ms) * 1 +
           parseInt(s) * 1000 +
           parseInt(m) * 60 * 1000 +
           parseInt(h) * 60 * 60 * 1000;
}

function renderTime(ms) {
    const h = Math.floor(ms / (60 * 60 * 1000)); ms -= h * 60 * 60 * 1000;
    const m = Math.floor(ms / (60 * 1000)); ms -= m * 60 * 1000;
    const s = Math.floor(ms / (1000)); ms -= s * 1000;

    return `${h<10?'0':''}${h}:${m<10?'0':''}${m}:${s<10?'0':''}${s},${ms}`;
}


// subtracting 10 seconds
let totalTime = getTotalMS('01:02:25,369');
totalTime -= 10 * 1000;
console.log(renderTime(totalTime));

probably could be optimized, but here's a starting point

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