question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Durations: Convert HH:MM:SS string to seconds

See original GitHub issue

Describe the bug A clear and concise description of what the bug is. I just wanted to know if it’s possible with Dayjs to Convert HH:MM:SS string to seconds. and if it’s not possible would you consider implementing this feature? (I’m available for this feature)

The feature can be used like this:

  const duration = dayjs.duration( '02:04:33', 'HH:mm:ss');

and the function would return something like the following:

const d = '02:04:33';   // your input string
const a = hms.split(':'); // split it at the colons

const result = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 
console.log(result);

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Soviutcommented, May 16, 2021

I’ve done the following

const time = '05:22:39'
const [hour, minute, second] = time.split(':').map((t) => parseInt(t))
const duration = dayjs.duration({ hour, minute, second })

The .map() is used to convert each split token to an integer.

2reactions
jamesastoundcommented, Nov 2, 2020

I would use a feature, like this. I develop for a video and audio production environment. Taking human formatted duration inputs and changing them to seconds, for calculation, is a very common task. I find durations most useful when summing (i.e. getting total duration of video clips to see if they will fit on a DVD). Using @hamzahamidi example, it would be helpful to have flexibility in the input format (“HH:mm” or “mm:ss”).

    <input class="duration" type="text" value="00:00:05" />
    <input class="duration" type="text" value="00:00:01" />
    <script>
      const total = [...document.querySelectorAll(".duration")]
        .map((time) => time.value)
        .reduce((acc, dur) => acc + dayjs().duration(dur, "HH:mm:ss"), 0);
      console.log(total); // 6
    </script>

Additional duration methods like average, max value, min value, etc, might also be useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert HH:MM:SS to seconds - Online tools
This tool converts human readable time using hours/minutes/seconds to seconds. You might also like the online Convert seconds to HH:MM:SS tool.
Read more >
Convert HH:MM:SS string to seconds only in javascript
== 60 seconds... so.. Only solution I think of is to split the string in array and then multiply 3600 to hour and...
Read more >
Convert HH:MM:SS to Seconds using JavaScript | bobbyhadz
To convert `hh:mm:ss` to seconds, convert the hours to seconds, by multiplying by `60` twice. Convert the minutes to seconds by multiplying by...
Read more >
Converting hh:mm:ss into seconds - MATLAB Answers
I want to convert Time column ( HH:MM:SS) to min, there is a direct command time2num but it requires toolbox and it's paid.So...
Read more >
Converting seconds to time string & time ... - write - Corbpie
Converting seconds to a time string and time string to seconds with vanilla Javascript. The time string is in the format of hh:mm:ss...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found