How to convert seconds to time string using Moment.js

In this article, we will convert seconds to time string such as 10d 11h 44m 10s or 5h 10m 49s using Moment.js. Additionally, we need to use the moment-duration-format plugin to convert seconds to a time string.

Steps to convert seconds to time string

  1. Import the libraries
  2. Use the script
  3. Output

1. Import the libraries

Let’s import the moment-duration-format plugin along with the Moment.js library. Use the following scripts to convert the seconds to time string.

If you are using the NPM Package then add the following code in the page.

2. Use the script

After importing the libraries, use the following script to get the desired output.

3. Output

Let’s combine the above code together and see how it works. You will see the output in the console.


I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *