How to convert time and seconds in javascript

PHPz
Release: 2023-04-06 11:26:01
Original
2804 people have browsed it

In JavaScript, it is often necessary to convert time into seconds, which is convenient for calculation and processing. Below, we will discuss how to convert time to seconds, and how to convert seconds to time.

1. Convert time to seconds

The following is the JavaScript code to convert time to seconds:

function timeToSeconds(time) {
  var hhmmss = time.split(':');
  var hours = parseInt(hhmmss[0], 10);
  var minutes = parseInt(hhmmss[1], 10);
  var seconds = parseInt(hhmmss[2], 10);
  var totalSeconds = (hours * 60 * 60) + (minutes * 60) + seconds;
  return totalSeconds;
}
Copy after login

This function accepts a string parameter representing the time, such as " 12:30:45". The function splits a time string into hours, minutes, and seconds and converts them to numbers. It then converts them to total seconds using the following formula:

Total seconds = hours x 60 x 60 minutes x 60 seconds

The result returned is an integer representing the total number of seconds .

2. Convert seconds to time

The following is the JavaScript code to convert seconds to time:

function secondsToTime(seconds) {
  var hours = Math.floor(seconds / 3600);
  var minutes = Math.floor((seconds - (hours * 3600)) / 60);
  var seconds = seconds - (hours * 3600) - (minutes * 60);
  var time = hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0');
  return time;
}
Copy after login

This function accepts an integer parameter representing the number of seconds. The function converts seconds to hours, minutes, and seconds using the following formula:

hours = seconds ÷ 3600
minutes = (seconds - (hours x 3600)) ÷ 60
seconds = seconds Number - (hours x 3600) - (minutes x 60)

Next, the function converts these values ​​into a string representing the time, such as "12:30:45". This string concatenates the hours, minutes, and seconds and adds a zero in front of them to ensure they always have two digits.

3. Example

The following is an example of how to use these functions in JavaScript:

var time = '03:45:12';
var seconds = timeToSeconds(time);
console.log("总秒数:" + seconds);

var totalSeconds = 12345;
var time = secondsToTime(totalSeconds);
console.log("时间:" + time);
Copy after login

This will output:

Total seconds: 13512
Time: 03:35:12

These functions are very useful because they can convert times into computable form and seconds into readable form. These functions can make the task of dealing with time very simple when writing JavaScript applications.

The above is the detailed content of How to convert time and seconds in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!