Search

Convert Hours into Seconds in JavaScript

  • Share this:

To convert hours into seconds in JavaScript, you can use the following approach:

Step 1: First, define a function that takes an argument `hours`. This argument will be the number of hours that you want to convert to seconds.

function convertToSeconds(hours) {
  // code to convert hours to seconds
}

Step 2: Inside the function, use the following formula to convert hours to seconds:
seconds = hours * 3600

function convertToSeconds(hours) {
  var seconds = hours * 3600;
  // return the result
}

Step 3: Finally, use the `return` statement to return the `seconds` variable.

function convertToSeconds(hours) {
  var seconds = hours * 3600;
  return seconds;
}

Here is the complete code:

function convertToSeconds(hours) {
  var seconds = hours * 3600;
  return seconds;
}

var result = convertToSeconds(2);
console.log(result); // Output: 7200

You can also use the following shortened version of the function:

function convertToSeconds(hours) {
  return hours * 3600;
}

var result = convertToSeconds(2);
console.log(result); // Output: 7200

 

Tags:

About author
I am a professional web developer. I love programming and coding, and reading books. I am the founder and CEO of StorialTech.