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

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

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 Report

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 Report

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 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!