Reputation: 325
I am trying to calculate the difference between two dates in Python, and express this time difference in days, weeks, months and years.
My Python code is as follows:
purDate = datetime.datetime.strptime(queryPurchaseDate (purID), "%Y-%m-%d %H:%M:%S")
now = datetime.datetime.now()
print 'Purchase date/time : ',purDate.strftime("%Y-%m-%d %H:%M:%S")
print 'Current date/time : ',now.strftime("%Y-%m-%d %H:%M:%S")
hold = now - purDate
print hold
The result of this code is as follows:
bash-3.2$ ./test.py
Purchase date/time : 2017-10-10 00:00:00
Current date/time : 2017-10-14 17:33:39
4 days, 17:33:39.866069
bash-3.2$
Instead of having the difference between the two date expressed as 4 days, 17:33:39.866069 I would like to have it expressed as follows: 4.65 Days, or 0.66 Weeks, or 0.15 Months, or 0.01 Years
Upvotes: 0
Views: 6004
Reputation: 5696
You can convert to a timedelta
object using pandas and then perform the subsequent mathematical operation
delta = pd.Timedelta(delta)
delta.total_seconds()/(24*60*60) # this will give the timedelta in days
Demo
import pandas as pd
import datetime
time1 = datetime.datetime.now()
time2 = '2017-10-01 00:00:00'
time2 = pd.to_datetime(time2)
delta = time1 - time2
print(delta)
13 days 22:03:50.081000
delta = pd.Timedelta(delta)
print(delta.total_seconds()/(24*60*60))
13.909289537037038
EDIT:
No need to use pandas. As @Reti43 explains, you can just use delta.total_seconds()/(24*60*60)
Upvotes: 4
Reputation: 9796
The difference between two datetime
objects is a timedelta
object. You can express that difference in seconds and then use a definition of total seconds in a day/week/month/year to get your ratio.
>>> diff = timedelta(days=4, seconds=17 * 3600 + 33 * 60 + 39.866069)
>>> print(diff)
4 days, 17:33:39.866069
>>> diff.total_seconds() / 86400
4.731711412835648
>>> diff.total_seconds() / (86400 * 7)
0.6759587732622354
>>> diff.total_seconds() / (86400 * 30)
0.15772371376118827
>>> diff.total_seconds() / (86400 * 365)
0.012963592911878487
This approach is slightly simplistic, in that it doesn't take into account the irregularity of the months or leap years. The difference between 365 and 366 days should not show up within the accuracy you desire, so you can ignore that. For the months, do you want 30 days to be one month consistently, or would you prefer, for example, from 01-08-2017 to 01-09-2017 to register as one month? That would complicate things for at most 0.01 improved accuracy.
def timedelta_to(time_diff, unit='day'):
options = {'day': 86400,
'week': 86400 * 7,
'month': 86400 * 30,
'year': 86400 * 365,
}
# default to days for an invalid unit of choice,
# though warning to user might be another option
if unit not in options:
unit = 'day'
duration = time_diff.total_seconds() / options[unit]
return '{0:.2f} {1}s'.format(duration, unit.title())
And you can use it like this.
>>> timedelta_to(diff)
'4.73 Days'
>>> timedelta_to(diff, 'month')
'0.16 Months'
Upvotes: 6
Reputation: 610
You may find a similar solution here:
python how to convert datetime dates to decimal years
Other option includes to separate time and date. In that case you can convert the time part into days and add it to the number of days.
Upvotes: 2