How to Convert Seconds to Days, Hours, Minutes and Seconds in Javascript?

Convert Seconds to Days, Hours, Minutes and Seconds in JS

Learning to convert seconds to days, hours, minutes and seconds in Javascript has several practical applications like real-time and countdown apps. You can use either conversion formulas or Date class in Javascript to handle it. Following the explanation below to understand better.

Convert seconds to days, hours, minutes and seconds in Javascript

Conversion formulas

To use some conversion formulas below, you should remember the following:

1 minute = 60 seconds
1 hour = 60 minutes = 3600 seconds
1 day = 24 hours = 86400 seconds

Then with n seconds, the days, hours, minutes, and seconds will be calculated like this:

  • Get the number of days by dividing n by 86400 because 1d = 86400s.
  • After getting the days, the remaining seconds will be n = n % 86400.
  • Get the number of hours by dividing n by 3600 because 1h = 3600s.
  • After getting the hours, the remaining seconds will be n = n % 3600.
  • Get the number of minutes by dividing n by 60 because 1m = 60s.
  • After getting the minutes, the remaining seconds will be n = n % 60, which will be the number of seconds.

To familiarize yourself with some conversion formulas, you can read this article.

Implement code

function convertSecsToDays(seconds) {
	let days = parseInt(seconds / 86400);
	return days;
}

function convertSecsToHours(seconds) {
	let hours = parseInt(seconds / 3600);
	return hours;
}

function convertSecsToMinutes(seconds) {
	let minutes = parseInt(seconds / 60);
	return minutes;
}

let seconds = 129600;
let numOfDays = convertSecsToDays(seconds);
seconds = seconds % 86400; // Remaining second after getting the days
let numOfHours = convertSecsToHours(seconds);
seconds = seconds % 3600; // Remaining second after getting the hours
let numOfMinutes = convertSecsToMinutes(seconds);
let numOfSeconds = seconds % 60; // Remaining second after getting the minutes and also the result for the number of seconds

console.log(
	`${numOfDays} days ${numOfHours} hours ${numOfMinutes} minutes ${numOfSeconds} seconds`
);

Output

"1 days 12 hours 0 minutes 0 seconds"

You may have noticed that the parseInt() function was used in 3 converter functions above, as dividing might cause the number to contain decimal values. Hence the parseInt() will return an integer.

Date() constructor

The Date() constructor returns a date object. It is specified as the number of milliseconds that have elapsed since the UNIX epoch – the UNIX operating system is being developed (midnight at the beginning of January 1, 1970). The constructor can be called with one argument, which is milliseconds.

Syntax

new Date(milliseconds)

Note that the milliseconds you pass in will be plus (minus) with January 01, 1970, 00:00:00. For example:

new Date(1000000000) => January 01 1970 00:00:00 + 1000000000 milliseconds
new Date(-1000000000) => January 01 1970 00:00:00 - 1000000000 milliseconds

Implement code

function convertSecondsToFormat(seconds) {
	let milliseconds = seconds * 1000; // Convert second to milliseconds
	let dateObj = new Date(milliseconds);
	let numOfHours = dateObj.getUTCHours();
	let numOfMinutes = dateObj.getUTCMinutes();
	let numOfSeconds = dateObj.getUTCSeconds();
	let hoursToSeconds = numOfHours * 60 * 60; // 1h = 60m, 1m = 60s
	let minutesToSeconds = numOfMinutes * 60; // 1m = 60s

	// The remaining seconds after getting the hours, minutes, and seconds
	let remainSeconds =
		seconds - (hoursToSeconds + minutesToSeconds + numOfSeconds);
	let numOfDays = parseInt(remainSeconds / 86400); // 1 day = 86400s
  
	console.log(
		`${numOfDays} days ${numOfHours} hours ${numOfMinutes} minutes ${numOfSeconds} seconds`
	);
}

let seconds = 129600;
convertSecondsToFormat(seconds);

Output

"1 days 12 hours 0 minutes 0 seconds"

Notice some methods contain the “UTC” keyword. It stands for Coordinated Universal Time, describing time zones depending on each timezone’s longitude. In this context, the methods will return a value in the time zone 0 (UTC +0), not a local timezone in your computer. If you have any questions, please feel free to leave us a message below.

Summary

To convert seconds to days, hours, minutes and seconds in Javascript, you can use either conversion formulas or the Date() constructor, but the Date class is the best way because it contains more methods to help you convert seconds into many formats. If you have any questions, please feel free to leave us a comment below.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *