How to Convert Seconds to Years with Python
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 of
import time seconds = time.time() print(seconds)
- Divide by 60 to get the number of minutes:
minutes = seconds / 60 print(minutes)
- Divide by 60 to get the number of hours:
hours = minutes / 60 print(hours)
- Divide by 24 to get the number of days:
days = hours / 24 print(days)
- Divide by 365.25 to get the number of years:
years = days / 365.25 print(years)
Or you can do it all in one step, like this: