How to Convert Days to Hours, Minutes, Seconds, or Milliseconds in JavaScript
In programming, you often need to convert between different units of time. A common requirement is to convert a number of days into a smaller unit like hours, minutes, seconds, or milliseconds, which are frequently used in APIs for setting timeouts, expirations, or durations. These conversions are straightforward mathematical operations.
This guide will teach you how to create a set of clean, reusable functions to convert a number of days into these smaller time units. We will also cover the best practice of using constants to make your code more readable and maintainable.
The Core Concept: Conversion Factors
The key to any unit conversion is knowing the correct multiplication factor. To convert from a larger unit (like days) to a smaller unit (like seconds), you multiply.
The basic relationships are:
- 1 day = 24 hours
- 1 hour = 60 minutes
- 1 minute = 60 seconds
- 1 second = 1000 milliseconds
To convert days to seconds, for example, you would multiply: days * 24 * 60 * 60.
Using Constants for Readability
Instead of using "magic numbers" like 24, 60, and 1000 directly in your formulas, it's a best practice to define them as constants. This makes your code self-documenting and easier to read.
const SECONDS_IN_A_MINUTE = 60;
const MINUTES_IN_AN_HOUR = 60;
const HOURS_IN_A_DAY = 24;
const MILLISECONDS_IN_A_SECOND = 1000;
const MILLISECONDS_IN_A_DAY =
HOURS_IN_A_DAY *
MINUTES_IN_AN_HOUR *
SECONDS_IN_A_MINUTE *
MILLISECONDS_IN_A_SECOND; // 86,400,000
Using these constants, our formulas become much more expressive and less prone to typos.
Converting Days to Milliseconds
This is a very common conversion, as many JavaScript functions (like setTimeout or the Date object) work with milliseconds.
Problem: you need to set a cookie to expire in 7 days, and the API requires the duration in milliseconds.
Solution:
const MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000;
function daysToMilliseconds(days) {
return days * MILLISECONDS_IN_A_DAY;
}
// Example Usage:
const sevenDaysInMs = daysToMilliseconds(7);
console.log(sevenDaysInMs); // Output: 604800000
Converting Days to Seconds
Problem: you have a cache that should store data for 1 day, and the system expects a TTL (Time To Live) value in seconds.
Solution:
const SECONDS_IN_A_DAY = 24 * 60 * 60;
function daysToSeconds(days) {
return days * SECONDS_IN_A_DAY;
}
// Example Usage:
const oneDayInSeconds = daysToSeconds(1);
console.log(oneDayInSeconds); // Output: 86400
Converting Days to Minutes
Problem: you are calculating the total duration of a multi-day event in minutes.
Solution:
const MINUTES_IN_A_DAY = 24 * 60;
function daysToMinutes(days) {
return days * MINUTES_IN_A_DAY;
}
// Example Usage:
const threeDaysInMinutes = daysToMinutes(3);
console.log(threeDaysInMinutes); // Output: 4320
Handling Fractional Days
If your input can be a decimal (e.g., 2.5 days), the result of the conversion will also be a decimal. In many cases, you may need to round this to the nearest whole number.
function daysToSeconds(days) {
return days * 24 * 60 * 60;
}
const result = daysToSeconds(1.573);
console.log(result); // Output: 135925.2
The Math.round() function rounds a number to the nearest integer. This is a good general-purpose choice for removing the decimal part.
function daysToSeconds(days) {
const seconds = days * 24 * 60 * 60;
return Math.round(seconds);
}
const result = daysToSeconds(1.573);
console.log(result); // Output: 135925
Other rounding methods like Math.floor() (always round down) or Math.ceil() (always round up) can also be used depending on your specific requirements.
Conclusion
Converting days to smaller time units is a matter of simple multiplication, but how you structure the code can greatly impact its readability and maintainability.
- The core of the conversion is the formula:
days * hours * minutes * seconds * milliseconds. - It is a best practice to use named constants (e.g.,
HOURS_IN_A_DAY) to make your formulas self-documenting and prevent the use of "magic numbers." - If your input can include fractional days, use
Math.round()to get an integer result.
By creating small, reusable helper functions for these conversions, you can make your time-based logic clean, accurate, and easy to understand.