Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried a sample code with 2 datetime inputs and I calculated the difference between them in minutes. It is working fine but I am not able to subtract every input in columns of excel. Please help.

What I have tried:

from datetime import datetime
date_formats = ["%Y-%m-%d %H:%M:%S", "%d-%m-%Y %H:%M:%S", "%m-%d-%Y %H:%M:%S", "%Y/%m/%d %H:%M:%S", "%d/%m/%Y %H:%M:%S", "%m/%d/%Y %H:%M:%S", "%Y.%m.%d %H:%M:%S", "%d.%m.%Y %H:%M:%S", "%m.%d.%Y %H:%M:%S", "%d %b %Y %H:%M:%S", "%b %d %Y %I:%M%p", "%y-%m-%d %H:%M:%S", "%d-%m-%y %H:%M:%S", "%m-%d-%y %H:%M:%S", "%y/%m/%d %H:%M:%S", "%d/%m/%y %H:%M:%S", "%m/%d/%y %H:%M:%S", "%y.%m.%d %H:%M:%S", "%d.%m.%y %H:%M:%S", "%m.%d.%y %H:%M:%S", "%b %d %Y %H:%M:%S", "%Y-%m-%d %H:%M", "%d-%m-%Y %H:%M", "%m-%d-%Y %H:%M", "%d-%m-%Y %H:%M", "%B %d %Y %H:%M:%S"]
#match = []
d1 = input()
d2 = input()
for fmt in date_formats:
try:
a = datetime.strptime(d1, fmt)
b = datetime.strptime(d2, fmt)
d = (b-a)
day = d.days
#print(d)
minutes = day * 24 * 60
print(minutes,'minutes')
break
except ValueError as e:
continue
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900