Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert time interval given in seconds into more human readable form

I need a code snippet for converting amount of time given by number of seconds into some human readable form. The function should receive a number and output a string like this:

34 seconds  12 minutes  4 hours  5 days  4 months 1 year 

No formatting required, hard-coded format will go.

like image 958
Dan Avatar asked Nov 21 '11 12:11

Dan


People also ask

How do you convert time to seconds in Python epoch?

To convert a datetime to seconds, subtracts the input datetime from the epoch time. For Python, the epoch time starts at 00:00:00 UTC on 1 January 1970. Subtraction gives you the timedelta object. Use the total_seconds() method of a timedelta object to get the number of seconds since the epoch.

How do you convert dates to seconds?

To convert a date to seconds:Create a Date object using the Date() constructor. Get a timestamp in milliseconds using the geTime() method. Convert the result to seconds by dividing by 1000 .

How do you convert time to seconds in react JS?

javascript1min read const timeString = "08:35:42"; Now, we need to convert the above format into seconds. To convert a hh:mm:ss format to seconds, first we need to split the string by colon : and multiply the hour value with 3600 and minutes value with 60 then we need to add everything to get the seconds.


1 Answers

 function secondsToString(seconds) { var numyears = Math.floor(seconds / 31536000); var numdays = Math.floor((seconds % 31536000) / 86400);  var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600); var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60); var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60; return numyears + " years " +  numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";  } 
like image 63
Royi Namir Avatar answered Sep 18 '22 13:09

Royi Namir