Python converts string time to datetime to calculate time difference

Mainly use datetime.strptime() This function does not talk nonsense about the code:

from datetime import datetime

t1='2022-4-17 19:00:00'
t2='2022-4-17 19:00:03'

# 这里是上面的字符串时间格式(可以改)
fmt = '%Y-%m-%d %H:%M:%S'

a=datetime.strptime(t1, fmt)
b=datetime.strptime(t2, fmt)

print((b-a).seconds)

输出结果为 3

Give it a thumbs up if it's useful hahahahahaha

Guess you like

Origin blog.csdn.net/qq_45064423/article/details/124234286