Python: convert dateelta to int time difference value

I want to change the time delta to an integer value.

My code is as follows.

import datetime
now = datetime.date.today()
print(now.toordinal()) # 736570
cali_date = datetime.data(2017, 6, 14)
print(cali_date.toordinal()) # 736494
date1 = now - cali_date
print(date1) # 76 days, 0:00:00

But I want to get only 76with an integer. How can I solve this problem? Thank.

+4
source share
2 answers

Just specify the attribute of the days timedeltaobject that you have:

print(date1.days)

There are also attributes timedelta.secondsand timedelta.microsecondsmodeling the complete state of the delta.

+5
source

date1- timedelta object - use date1.daysto get the number of days as an integer, or date1.total_seconds()to see the number of seconds between two datetime objects.

+4
source

Source: https://habr.com/ru/post/1684690/


All Articles