Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: convert datedelta to int value of time difference

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.

like image 616
Tom Avatar asked Aug 29 '17 18:08

Tom


People also ask

How do I convert Timedelta to time in python?

There are two different ways of doing this conversion: the first one you divide the total_seconds() by the number of seconds in a minute, which is 60. the second approach, you divide the timedelta object by timedelta(minutes=1)


2 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.

like image 78
Martijn Pieters Avatar answered Nov 15 '22 15:11

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.

like image 30
jsbueno Avatar answered Nov 15 '22 16:11

jsbueno