Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!