2
import datetime
birthday = datetime.datetime(1996,8,15)
differnce = datetime.datetime.now() - birthday
#This returns a timedelta object -> datetime.timedelta(7629, 36148, 156646)

Now i want to convert precisely the timedelta object in years, months, days and seconds.. I know i can do like difference.days // 365 to get years and so on.. Is there any other way or methods to convert this timedelta objects in Year, months, days and minutes precisely and easily?

Mohit Solanki
  • 2,122
  • 12
  • 20
  • No, `datetime.timedelta` is difference between datetime objects. We can get days and seconds from `timedelta`. As you mention, we need to do arithmetic to convert in year , month and day formats.(leap year?) – Vivek Sable Jul 05 '17 at 05:03
  • If you want to take leap years into account, I don't think an external module exists, though I could be wrong. –  Jul 05 '17 at 05:14

3 Answers3

1

I think you probably want to use dateutil.relativedelta. Here is a SO post explaining how to use it:

Python timedelta in years

If this isn't what you're looking for then providing some example output in your question might help.

melchoir55
  • 6,842
  • 7
  • 60
  • 106
1

Adding with the question comment timedeltagives you days and seconds. So if you want to calculate the year than you should perform some simple arithmetic calculation. Here Is simple way to calculate the year. some code adding your given code.

import datetime
import math
birthday = datetime.datetime(1996,8,15)
differnce = datetime.datetime.now() - birthday
year = differnce.days//(365.25)
month = (differnce.days-year*365.25)//(365.25/12)
day = ((differnce.days-year*365.25) - month*(365.25/12))
print('Years=',int(year),' Months=',int(month), ' Days=',int(math.ceil(day)))

here the 365.25 is used for almost correctly for leap year calculation. For more details check here and here.

R.A.Munna
  • 1,699
  • 1
  • 15
  • 29
0

You could use this function:

import datetime
import calendar

def difference(start, end):
    years = end.year - start.year
    months = end.month - start.month
    days = end.day - start.day
    hours = end.hour - start.hour
    minutes = end.minute - start.minute
    seconds = end.second - start.second
    if seconds < 0:
        minutes -= 1
        seconds += 60
    if minutes < 0:
        hours -= 1
        minutes += 60
    if hours < 0:
        days -= 1
        hours += 24
    if days < 0:
        months -= 1
        days += calendar.monthrange(start.year, start.month)[1]
    if months < 0:
        years -= 1
        months += 12
    return { 
        'years': years, 
        'months': months, 
        'days': days,
        'hours': hours,
        'minutes': minutes,
        'seconds': seconds
    }


birthday = datetime.datetime(1996,8,30)
diff = difference(birthday, datetime.datetime.now())
print(diff)

This counts the years (including leap years) as they occur between the two dates.

The difference in days is either the net difference of the day numbers, or -- if the current date has a smaller day number than the birth date -- then the number of days remaining in the month of birth are added to the current day number. This can be a bit arbitrary. Suppose tomorrow is my birthday (and the birthday is not on the first of the month), then the output will have 11 for the months as expected, but either 27, 28, 29 or 30 for the days component, depending how many days the month of birth has...

trincot
  • 317,000
  • 35
  • 244
  • 286