简体   繁体   中英

The error of subtract between date in Python

I got some date from mongodb on python.
And there are two dates as below.

// type of values
<class 'datetime.datetime'>
<class 'datetime.datetime'>

// values
2020-01-27 21:07:16.755000 // pick-up
2020-01-27 20:10:00 // drop

When the difference of the dates is positive, it gives this. This is correc answer.

pick-up - drop = 0:57:16.755000

But When the difference of date is negative, it gives werid value.

drop - pick-up = -1 day, 23:02:43.245000

I hope to get this value.

drop - pick-up = -0:57:16.755000

How can I get this? Thank you so much for reading it.

Only date values are negative, hours -- positive. To get negative seconds use:

(drop - pick-up).total_seconds()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM