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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to convert days into human-readable years, months and days in Python

2 Answers

0 votes
import datetime
from dateutil.relativedelta import relativedelta

# Difference between Jan 1, 2024 and Mar 27, 2025 (including both days):
# 1 year 2 months 27 days
# or 14 months 27 days
# or 64 weeks 4 days
# or 452 calendar days

days = 452
start_date = datetime.date(2024, 1, 1)
end_date = start_date + datetime.timedelta(days=days)

diff = relativedelta(end_date, start_date)

print(f"{diff.years} years {diff.months} months {diff.days} days")
 
 
 
'''
run:
 
1 years 2 months 27 days
 
'''

 



answered Jun 27, 2024 by avibootz
edited Dec 31, 2025 by avibootz
0 votes
from datetime import date, timedelta

def days_to_ymd(days: int) -> str:
    start = date(1970, 1, 1)
    end = start + timedelta(days=days)
    diff = end - start

    # Use relativedelta for calendar-accurate Y/M/D
    from dateutil.relativedelta import relativedelta
    rd = relativedelta(end, start)

    return f"{rd.years} year{'s' if rd.years != 1 else ''}, " \
           f"{rd.months} month{'s' if rd.months != 1 else ''} and " \
           f"{rd.days} day{'s' if rd.days != 1 else ''}"

print(days_to_ymd(452))



'''
run:

1 year, 2 months and 28 days

'''

 



answered Dec 31, 2025 by avibootz

Related questions

...