In this article we will learn how to convert days to seconds in JavaScript.
It is very common to convert days to seconds in JavaScript in JavaScript.
Problem
Now we have a problem, and it is that we want to convert days into seconds.
For example, if we have the number of days is three, then it will be in hours like this:
4320
And if we have the number of days is five:
7200
How to solve this problem?
This problem is very simple, all that is involved is a simple mathematical operation, to convert days into seconds first we have to multiply by 24 which is the number of hours and then multiply by 60 which is the number of minutes then multiply by 60 again which is the number of seconds then you will get the result.
The solution will be as follows:
- multiply by 24 which is the number of hours
- multiply by 60 which is the number of minutes
- then multiply by 60 again which is the number of seconds
Solution:
Let's see an example of solution:
// Define variables
const days = 5;
const hours = 24;
const min = 60;
const sec = 60;
// Get five days in minutes
console.log(days * hours * min * sec);
Output
432000
Now you will probably use the code more than once, so we will create a function that calculates that result for us
Function
// Create a function
function convertToSecs(days) {
const hours = 24;
const min = 60;
const sec = 60;
return days * hours * min * sec;
}
// Tests
console.log(convertToSecs(5));
console.log(convertToSecs(3));
console.log(convertToSecs(10));
Output
432000
259200
864000
Thank you for reading
Thank you for reading my blog. 🚀