Convert Seconds to Years, Days, Hours, and Minutes in JavaScript



In this article, we create a function that takes in a number(num) which represents the number of seconds. So, it construct and return a string that contains information of years, days, hours and minutes contained in those seconds.

Converting seconds into years, days, hours, and minutes in JavaScript involves breaking down a given total of seconds into more understandable time units. This process helps display elapsed time in a readable format.

For the purpose of the article, we will consider that all the years have 365 days.

Let us understand through some sample example of I/O Scenario ?

Sample Input -

const num = 5454776657;

Sample Output -

const output = '172 years, 353 days, 23 hours, 44 minutes and 17 seconds';

Converting seconds in years days hours and minutes

Converting seconds in years days hours and minutes in JavaScript is quite easy. Let's learn through the following programs ?

Using Math Function

The Math function are operations provided by JavaScript's built-in Math object to perform various mathematical calculations.

Example

In this program, The function 'convertSeconds' converts a given number of seconds into years, days, hours, minutes, and remaining seconds. It uses mathematical calculations to divide the total seconds into each time unit and format the results into a readable string.

function convertSeconds(seconds) {
  const years = Math.floor(seconds / (365 * 24 * 60 * 60));
  const days = Math.floor((seconds % (365 * 24 * 60 * 60)) / (24 * 60 * 60));
  const hours = Math.floor((seconds % (24 * 60 * 60)) / (60 * 60));
  const minutes = Math.floor((seconds % (60 * 60)) / 60);
  const remainingSeconds = seconds % 60;

  return `${years} years, ${days} days, ${hours} hours, ${minutes} minutes and ${remainingSeconds} seconds`;
}

console.log(convertSeconds(5454776657));

Output

The above program produce the following result ?

172 years, 353 days, 23 hours, 44 minutes and 17 seconds

Using Date Object

The Date object is used to work with dates and times. It allows for creating date and time instances by retrieving and manipulating the date and time values.

Example

The function 'convertSeconds' transforms seconds into a formatted string of years, days, hours, minutes, and seconds.It uses the 'Date' object for detailed time calculations and compute years and remaining days.

function convertSeconds(seconds) {
  const date = new Date(seconds * 1000);
  const years = Math.floor(seconds / (365 * 24 * 60 * 60));
  const days = date.getUTCDate() - 1;
  const hours = date.getUTCHours();
  const minutes = date.getUTCMinutes();
  const remainingSeconds = date.getUTCSeconds();

  return `${years} years, ${days} days, ${hours} hours, ${minutes} minutes and ${remainingSeconds} seconds`;
}

console.log(convertSeconds(5454776657));

Output

Following is the output of the above program ?

172 years, 7 days, 23 hours, 44 minutes and 17 seconds

Using Recursive Function

A recursive function is a function that calls by itself to solve problems by breaking them into smaller subproblems. It includes a base case to stop the recursion and prevent infinite loops.

Example

The function 'convertSeconds' recursively converts seconds into a readable string of time units, using arrays for time divisions and concatenating results.

function convertSeconds(seconds, units = ['years', 'days', 'hours', 'minutes', 'seconds'], divisors = [365*24*60*60, 24*60*60, 60*60, 60, 1]) {
  if (units.length === 0) return '';

  const unit = units.shift();
  const divisor = divisors.shift();
  const value = Math.floor(seconds / divisor);
  const remainder = seconds % divisor;

  return `${value} ${unit}${value !== 1 ? '' : ''}, ` + convertSeconds(remainder, units, divisors);
}

console.log(convertSeconds(5454776657));

Output

Following is the output of the above program ?

172 years, 353 days, 23 hours, 44 minutes, 17 seconds 

Using Custom Time Formatter

The custom time formatter are functions or methods that allow you to format time values into human-readable strings based on specific requirements or preferences.

Example

The function 'convertSeconds(seconds)' converts seconds into a readable string of years, days, hours, minutes, and seconds, ensuring proper formatting and pluralization.

function convertSeconds(seconds) {
  if (seconds < 1) return '0 seconds';

  const units = [
    { label: 'year', value: 365 * 24 * 60 * 60 },
    { label: 'day', value: 24 * 60 * 60 },
    { label: 'hour', value: 60 * 60 },
    { label: 'minute', value: 60 },
    { label: 'second', value: 1 }
  ];

  const results = units.map(unit => {
    const quotient = Math.floor(seconds / unit.value);
    seconds %= unit.value;
    return quotient > 0 ? `${quotient} ${unit.label}${quotient > 1 ? 's' : ''}` : '';
  }).filter(Boolean);

  const last = results.pop();
  return results.length ? `${results.join(', ')} and ${last}` : last;
}

console.log(convertSeconds(5454776657));

Output

Following is the output of the above program ?

172 years, 353 days, 23 hours, 44 minutes and 17 seconds
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-02-10T11:11:08+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements