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.
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.
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 .
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.
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"; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With