luka032
luka032

Reputation: 955

Convert time format to string and inverse

How could I convert number of seconds (number) to time format hh:mm:ss string, and reverse thing hh:mm:ss time format string back to number that is equal to number of seconds?

I.E:

2500 -> 00:41:40

And

00:41:40 -> 2500

using JavaScript?

Upvotes: 0

Views: 1081

Answers (3)

liontass
liontass

Reputation: 740

Here is another approach via two functions

function secondsToTime(seconds) {
  let hours = Math.floor(seconds / 3600);
  let minutes = Math.floor((seconds % 3600) / 60);
  let remainingSeconds = seconds % 60;

  return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
}

function timeToSeconds(time) {
  const [hours, minutes, seconds] = time.split(':').map(Number);
  return hours * 3600 + minutes * 60 + seconds;
}


console.log(secondsToTime(2500));

console.log(timeToSeconds("00:41:40"));

Upvotes: 0

Harsh Patel
Harsh Patel

Reputation: 1

This solution is not as documented well but this might be your answer to whatever format you want time to be.

const time = "2h 25m";

function convertStringTimeToSeconds(stringTime) {

  let dur = stringTime.split(" ").map((t) => t.split(""));

  for (let i = 0; i < dur.length; i++) {

    const e = dur[i];

    e.pop();

  }

  let [hr, min, sec] = dur;

  hr = hr.join("");

  min = min.join("");

  sec = sec ? .join("") ? ? 0;

  for (let i = 0; i < dur.length; i++) {

    const e = dur[i];

    if (e.length > 1) {

      dur[i] = [e.join("")];

    }

  }

  return hr * 3600 + min * 60 + sec;

}

console.log(convertStringTimeToSeconds(time)); //8700

const timeNumber = convertStringTimeToSeconds(time)

function reverseTimeToString(numTime) {

  const time = (numTime / 3600).toFixed(2)

  const [hr, min] = time.split(".")

  const toMin = (60 / (100 / min)).toFixed(0)

  console.log(hr, toMin)

  return hr + "h" + " " + toMin + "m";

}

console.log(reverseTimeToString(timeNumber)); //2h 25m

Upvotes: 0

Daniel Grant
Daniel Grant

Reputation: 156

I don't think there is a function directly for this purpose, but you can easily make one for yourself. Time to seconds is the easier part:

function timetosec(time)
{
   var h = time.split(':')[0];
   var m = time.split(':')[1];
   var s = time.split(':')[2];
   return h*60*60 + m*60 + s;
}

And just use the modulus operator, to change it back:

function sectotime(sec)
{
   var s = sec % 60; 
   sec = (sec-s)/60;
   var m = sec % 60;
   var h = (sec-m)/60;
   return '' + h + ':' + m + ':' + s;
}

Another idea: Use the Date object. According to the JS reference, you can pack in a string object like this:

   var d1 = new Date('2016-01-01 '.time); //e.g. '2016-01-01 01:02:03'
   var d2 = new Date('2016-01-01 00:00');
   return d2 - d1; // time in MILLIseconds

   var d1 = new Date(sec*1000); //milliseconds since the UNIX epoch
   return d1.getHours() + ':' + d1.getMinutes() + ':' + d1.getSeconds();

Upvotes: 1

Related Questions