How to turn a total number of seconds into years, months and days in Python

2 Answers

0 votes
def seconds_to_years_months_days(seconds):
    minute = 60
    hour = 60 * minute
    day = 24 * hour
    month = 30 * day
    year = 365 * day

    years, seconds = divmod(seconds, year)
    months, seconds = divmod(seconds, month)
    days, seconds = divmod(seconds, day)

    return years, months, days

print(seconds_to_years_months_days(10_000_000))



'''
run:

(0, 3, 25)

'''

 



answered Jan 20 by avibootz
0 votes
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

def seconds_to_years_months_days(seconds):
    start = datetime(1970, 1, 1)
    end = start + timedelta(seconds=seconds)
    diff = relativedelta(end, start)
    return diff.years, diff.months, diff.days

print(seconds_to_years_months_days(10_000_000))



'''
run:

(0, 3, 25)

'''

 



answered Jan 20 by avibootz

Related questions

...