Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

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

...