Borislav Hadzhiev
Fri Jan 28 2022·1 min read
Photo by Meiying Ng
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.
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.
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.
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.
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.
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.
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.