How to Convert Seconds to Years with Python

  • google plus

In Brief...

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.

Instructions

  1. Start with a number of seconds. time.time() will return something in the neighborhood of 1466604569.0708675:
    import time
    seconds = time.time()
    print(seconds)
    Output:
    1466604569.0708675
  2. Divide by 60 to get the number of minutes:
    minutes = seconds / 60
    print(minutes)
    Output:
    24443409.48451446
  3. Divide by 60 to get the number of hours:
    hours = minutes / 60
    print(hours)
    Output:
    407390.158075241
  4. Divide by 24 to get the number of days:
    days = hours / 24
    print(days)
    Output:
    16974.58991980171
  5. Divide by 365.25 to get the number of years:
    years = days / 365.25
    print(years)
    Output:
    46.47389437317374

Or you can do it all in one step, like this:

Python: Seconds to Years in One Step

Related Video

Related Training

Author: Nat Dunn

Nat Dunn founded Webucator in 2003 to combine his passion for web development with his business expertise and to help companies benefit from both.

Discuss