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
seconds = time.time()
- Divide by 60 to get the number of minutes:
minutes = seconds / 60
- Divide by 60 to get the number of hours:
hours = minutes / 60
- Divide by 24 to get the number of days:
days = hours / 24
- Divide by 365.25 to get the number of years:
years = days / 365.25
Or you can do it all in one step, like this:
Author: Nat Dunn
Nat Dunn is the founder of Webucator (www.webucator.com), a company that has provided training for tens of thousands of students from thousands of organizations. Nat started the company in 2003 to combine his passion for technical training with his business expertise, and to help companies benefit from both. His previous experience was in sales, business and technical training, and management. Nat has an MBA from Harvard Business School and a BA in International Relations from Pomona College.
Follow Nat on Twitter at @natdunn and Webucator at @webucator.