How to Convert Seconds to Years with Python
See Python: Tips and Tricks for similar articles.
The time() method of Python's time module returns the seconds since the epoch (1/1/1970 at midnight). To convert the number of seconds to years with Python, divide by seconds in a minute, minutes in an hour, hours in a day, and days in a year.
- Start with a number of seconds.
time.time()will return something in the neighborhood of1466604569.0708675:
Output:import time seconds = time.time() print(seconds)1466604569.0708675 - Divide by 60 to get the number of minutes:
Output:minutes = seconds / 60 print(minutes)24443409.48451446 - Divide by 60 to get the number of hours:
Output:hours = minutes / 60 print(hours)407390.158075241 - Divide by 24 to get the number of days:
Output:days = hours / 24 print(days)16974.58991980171 - Divide by 365.25 to get the number of years:
Output:years = days / 365.25 print(years)46.47389437317374
Or you can do it all in one step, like this:
