简体   繁体   中英

Difference between two different dates in python

I want to write a function that finds the difference between two different time stamps in the following form: "October 5th 2019, 10:13:24 am". The difference between the two times will rarely be more than 15 minutes, but some times may go between days or years. For example, the difference between "December 31st 2017, 11:58:26 pm" and "January 1st 2018, 00:04:56 am". I was trying to use datetime and strptime but I can't get it to work.

a = [{"time":"October 5th 2019, 10:13:24 am"},{"time":"October 5th 2019, 10:17:05 am"}] 

def difference(n): 
    for i in range(len(n)-1): 
        date1 = n[i].get("time") 
        date2 = n[i+1].get("time") 
        time1 = datetime.strptime(date1, '%m /%d /%y %H:%M:%S') 
        time2 = datetime.strptime(date2, '%m /%d /%y %H:%M:%S') 
    diff = time2 - time1 
    return diff

The table of all the formatting strings you can use with strptime is here: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior .

Note that it does have support to specify a month as a string like "October" instead of a number like "10", but you have to use "%B" instead of "%m". It's very important for the formatting string to exactly match the format of the input you're providing.

Similarly, you can specify a time as AM/PM instead of 24-hour, but you need to use "%I" and "%p" instead of "%H".

datetime.strptime doesn't handle a date in a format like "5th", so you have to do a little re.sub to remove that piece, but once you've stripped off the letters you can use "%d":

>>> from datetime import datetime
>>> import re
>>> a = [{"time":"October 5th 2019, 10:13:24 am"},{"time":"October 5th 2019, 10:17:05 am"}]
>>> def parse_date(date: str) -> datetime:
...     return datetime.strptime(
...         re.sub(r" (\d+)[a-z]+ ", r" \1 ", date),
...         "%B %d %Y, %I:%M:%S %p"
...     )
...
>>> parse_date(a[1]["time"]) - parse_date(a[0]["time"])
datetime.timedelta(seconds=221)

The difference between 2 datetime.datetime objects is represented with datetime.timedelta . From the official documentation :

A timedelta object represents a duration, the difference between two dates or times.

It holds only days, seconds, and microseconds . You can then process that information as you desire. For example,

>>> from datetime import date
>>> d1=date(2012, 12, 4)
>>> d2=date(2015,10,9)
>>> d1-d2 # the difference will be negative(past-future)
datetime.timedelta(-1039)
>>> d2-d1 # the difference is positive (future-past)
datetime.timedelta(1039)
>>> delta=d2-d1
>>> delta.days, delta.seconds,delta.microseconds
(1039, 0, 0) # no seconds and microseconds as the difference can be shown in days only

For processing that number and formatting it the right way you can look at this question on Stack Overflow.

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