Python: convert datedelta to int value of time difference

Question:

I want to change time delta to integer value .

My code is as below.

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 just 76 with integer.
How can I solve this problem?
Thank you.

Asked By: Tom

||

Answers:

Just reference the days attribute of the timedelta object you have there:

print(date1.days)

There are also timedelta.seconds and timedelta.microseconds attributes, modeling the complete delta state.

Answered By: Martijn Pieters

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

Answered By: jsbueno

For me worked using Serie.dt.days . It changes the datatype of a Serie from "timedelta" to "int" and the time difference is presented in full days.

Answered By: Marta

Just set a new variable:

date1 = now - cali_date
date_int = date1

print(type(date_int))
# <class 'int'>
Answered By: carsalves
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.