Convert Days to Minutes using JavaScript

avatar

Borislav Hadzhiev

Fri Jan 28 20221 min read

banner

Photo by Meiying Ng

Convert Days to Minutes using JavaScript #

To convert days to minutes, multiply the days by 24 for the hours and 60 for the minutes, e.g. days * 24 * 60. The result from the multiplication will represent the minutes equivalent for the number of days.

index.js
function daysToMinutes(days) { // 👇️ hour min return days * 24 * 60; } console.log(daysToMinutes(1)); // 👉️ 1440 console.log(daysToMinutes(2)); // 👉️ 2880 console.log(daysToMinutes(5)); // 👉️ 7200

We created a reusable function that takes the number of days as parameter and converts the days to minutes.

To convert the days to minutes, we have to multiply by 24 to convert to hours and 60 to convert to minutes.

If your application uses fractional units for the days, you might get a decimal number after the conversion.

index.js
function daysToMinutes(days) { // 👇️ hour min return days * 24 * 60; } console.log(daysToMinutes(3.43)); // 👉️ 4939.20000001

You can use the Math.round() function to round the number to the nearest integer.

index.js
function daysToMinutes(days) { // 👇️ hour min return Math.round(days * 24 * 60); } console.log(daysToMinutes(3.43)); // 👉️ 4939

We passed the value to the Math.round function to round to the nearest integer and make sure we don't get a decimal after the conversion.

Here are some examples of how the Math.round function works.

index.js
console.log(Math.round(2.49)); // 👉️ 2 console.log(Math.round(2.5)); // 👉️ 3

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5, it gets rounded to the lower absolute value.

Use the search field on my Home Page to filter through my more than 1,000 articles.