I get a date from a file in the following format:
2018-07-20 05:16:12
I need to compare this date with a file's last modified date. I can get the last modified date in the following format:
datetime.datetime.fromtimestamp(os.path.getmtime('test-reboot-3.txt'))
datetime.datetime(2018, 7, 27, 7, 58, 15, 541916)
How can I compare the latter date with the above mentioned date from my file?
You need to convert your string to datetime object and then compare.
Ex:
import datetime
d1 = "2018-07-20 05:16:12"
d1 = datetime.datetime.strptime(d1, "%Y-%m-%d %H:%M:%S")
d2 = datetime.datetime(2018, 7, 27, 7, 58, 15, 541916)
print(d2 - d1)
Output:
7 days, 2:42:03.541916
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.