简体   繁体   中英

How to get Difference between two strings with time and weekday in Python

I have 2 strings of weekday and time:

time_1 = "Wed 09:00"
time_2 = "Fri 13:00"

I want to get the time difference between the two times in seconds.

I have tried the datetime.datetime.strptime (or strftime ) but without success. it does not know how to relate to the weekday.

example:

time_1 = strptime(time_1, "%a %H:%M")
time_2 = strptime(time_2, "%a %H:%M")

time_diff = (time_2 - time_1).total_seconds() does not take into account the weekday, just the hours and minutes.

My case is that I don't parse dates, but weekdays and hours/minutes

This can be easily done using dateparse module in python.

Install dateparse module using pip

pip install dateparse

Import the module and calculate the time difference in seconds

from dateparser import parse

time_1 = "Wed 09:00"
time_2 = "Fri 13:00"

time_1 = parse(time_1)
time_2 = parse(time_2)

time_diff = (time_2 - time_1).total_seconds()

print(time_diff)

The output of the above code sample is 187200.0

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