Convert Seconds to Hours Minutes Seconds in Javascript

In this post, we will see how to convert seconds to Hours Minutes Seconds in JavaScript.

There are multiple ways to do it. Let’s go through them.

Using javascript without any external library

To convert seconds to Hours Minutes Seconds in javascript:

  • Create new date object with its constructor.
  • Pass seconds to setSeconds() method on date object.
  • Use toISOString() to convert date to ISOString format.
  • Use substr() to get string in HH:mm:ss format.

Output:

We created new Date object and called setSeconds() method on date object.

toISOString() method returns ISO 8601 string representation of Date – YYYY-MM-DDTHH:mm:ss.sssZ

Output:

We used substr() method to get HH:mm:ss part of String.

substr() methods takes 2 parameters

  • Start index: Start index of substring
  • Length of String: Length of substring.

As we were looking for string after T part of ISO format string, we have used substr with start index 11 and length of String as 8.

You can also use alternative method:

  • Multiply seconds with 1000.
  • Pass it to Date‘s constructor
  • Use toISOString() to convert it to ISOString format’
  • Use substr() to get string in HH:MM:SS format.

Output:

If number of seconds are less than 3600, you can remove hours part and format the string in minutes and seconds.

Output:

Using momentJs library

You can also use moment js library to get seconds to hours minutes seconds format.

Output:

That’s all about how to convert seconds to hours minutes seconds in JavaScript.

Was this post helpful?

Related Posts

  • 25 May

    Remove Double Quotes from String in JavaScript

    Table of ContentsWays to Remove Double Quotes from String in JavaScriptUsing replace()Using replaceAll()Using split() and join() methodUsing for loopConclusionWas this post helpful? In this post, we will see how to remove double quotes from String in JavaScript. Ways to Remove Double Quotes from String in JavaScript There are multiple ways to do it. Let’s go […]

  • 25 May

    Remove First Character from String in JavaScript

    Table of ContentsWays to Remove First Character from string in JavaScriptUsing substring()Using slice()Using substr()Using replace() methodUsing Array’s shift() with split() and join()ConclusionWas this post helpful? In this post, we will see how to remove first character from string in JavaScript. Since Strings are immutable in JavaScript, you can’t do it in-place. All the methods will […]

  • 25 May

    [Solved] TypeError: toLowerCase is not a function in JavaScript

    TypeError: .toLowerCase is not a function occurs when we call toLowerCase() function on object which is not an string. toLowerCase() function can be only called on string. To resolve this issue, convert value to string using toString() ,method before calling toLowerCase() method. Let’s see with help of simple example [crayon-62b8def5a419a603713523/] You will get following error […]

  • 25 May

    Escape Json String Containing Newline Characters in JavaScript

    Table of ContentsEscape JSON string in JavaScriptConclusionWas this post helpful? In this post, we will see how to escape JSON string containing newline characters using JavaScript. There is no well known JavaScript library which can escape all special characters in String. Escape JSON string in JavaScript There is no direct way to escape JSON String […]

  • 23 May

    Escape Apostrophe in JavaScript

    Table of ContentsWays to escape Apostrophe in JavaScriptUsing escape characterUsing alternate String syntaxUsing template literalsWas this post helpful? In this article, we will see how to escape Apostrophe in JavaScript. Let’s see what happens if we have singe quote in String and we also use single quotes to declare the string in JavaScript. [crayon-62b8def5a51b7565421757/] Output: […]

  • 23 May

    TypeError: foreach is not a function in JavaScript

    Table of ContentsUse Object.keys() or Object.values() or Object.entries()Use Arrays.from()Was this post helpful? TypeError: .foreach is not a function occurs when we call foreach() function on object which is not array, Set or Map. To resolve this issue, you can use Object.keys() rather than directly calling foreach() on object or use Array.from() to convert array like […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.