How to Turn Milliseconds into a Useful Object with JavaScript

Using the source code in the library parse-ms, I wanted to create my utility to create useful objects that turn milliseconds into understandable chunks. I tend not to like to use external libraries for utilities, so this pulls it out into your copy/paste chunk.

We will create the JavaScript utility that converts a given number of milliseconds into an object containing days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds.

We will first write some code and then talk about the solution:

Parsing Milliseconds

Next, we define the parseNumber function. This function takes a number representing milliseconds and returns an object with time components:

// Ensures that if we have an infinite value, we fallback to zero for predictable results
const toZeroIfInfinity = value => Number.isFinite(value) ? value : 0;

function parseMilliseconds(milliseconds) {
    return {
        days: Math.trunc(milliseconds / 86_400_000),
        hours: Math.trunc(milliseconds / 3_600_000 % 24),
        minutes: Math.trunc(milliseconds / 60_000 % 60),
        seconds: Math.trunc(milliseconds / 1000 % 60),
        milliseconds: Math.trunc(milliseconds % 1000),
        microseconds: Math.trunc(toZeroIfInfinity(milliseconds * 1000) % 1000),
        nanoseconds: Math.trunc(toZeroIfInfinity(milliseconds * 1e6) % 1000),

// For a number input
const duration = parseMilliseconds(183900000);
// Output:
// {
//   days: 2,
//   hours: 3,
//   minutes: 6,
//   seconds: 30,
//   milliseconds: 0,
//   microseconds: 0,
//   nanoseconds: 0
// }


First, there's a small utility function called toZeroIfInfinity. This function checks if a given number (value) is finite. If it is, it returns the number itself. If it's infinite, it returns 0. This helps prevent errors when dealing with extremely large numbers.

Next, there's the parseMilliseconds function. This function takes a duration in milliseconds and breaks it into a more readable object format with days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds. Here's how it works:

  • days is calculated by dividing the milliseconds by the number of milliseconds in a day (86,400,000) and removing the decimal part.
  • hours, minutes, and seconds are calculated similarly but using the remainder after dividing by the number of milliseconds in the larger units, ensuring they wrap around appropriately (e.g., hours wrap around after 24).
  • milliseconds is the remainder after dividing by 1,000.
  • microseconds and nanoseconds are calculated by multiplying the milliseconds by 1,000 and 1,000,000, respectively, and then taking the remainder after dividing by 1,000. The toZeroIfInfinity function ensures that if the calculations result in infinite values, they are replaced with 0.

Together, these functions help to transform a potentially unwieldy millisecond value into a structured and easily understandable format.

Another one for the utility folder!

Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses; Lead Developer, Software Architect, Product Manager, CTO and now happily a Founder.


Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.