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

233
Views
Subtracting seconds from the time in Javascript

I need to create a second function that will subtract recorded time from this in seconds. Currently, this function will display time as event_time: 02:24:25 which is great, I just need to take my recorded time in seconds e.g. 85 seconds and subtract from this value.

so if DateTimeFun is 02:24:25 and recordedTime = 60, the new function would return 02:23:25, subtracting 1 minute.

let recordedTime = parseInt(inputData.new_value) - parseInt(inputData.previous_value);

let dateTimeFun = function (d) {
    let h = d.getHours();
    let m = d.getMinutes();
    let s = d.getSeconds();

    if (h < 10) h = '0' + h;
    if (m < 10) m = '0' + m;
    if (s < 10) s = '0' + s;

    return h + ':' + m + ':' + s;
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A simple function to subtract seconds from a date:

let subtractSeconds = (d1, seconds) => {
  console.log(d1);

  const ms = d1.getTime();
  const ms2 = ms - seconds * 1000;
  const d2 = new Date(ms2);

  console.log(d2);

  return d2;
};

subtractSeconds(new Date(), 100);

So assuming d is a date, you can do:

dateTimeFun(subtractSeconds(d, secondsToSubtract));
about 4 years ago · Juan Pablo Isaza Report

0

const subtract = (JSDate, seconds) => new Date(JSDate - (seconds * 1000))

const now = new Date();
const date = subtract(now, 10);

console.log(now); // Thu Jan 27 2022 00:27:29 GMT-0300 (Uruguay Standard Time)
console.log(date); // Thu Jan 27 2022 00:27:19 GMT-0300 (Uruguay Standard Time)
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!