I was trying to caltculate the date using datetime module, but when I entered 1
in define dates
it showed me:
41 days, 0:00:00
Traceback (most recent call last):
File "", line 22, in <module>
print(dates(i))
^^^^^^^^
File "", line 18, in dates
SD = date(YY2, MM2, DD2)
^^^^^^^^^^^^^^^^^^^
ValueError: day is out of range for month
Strangely, when i entered 0
in define it gave me a nice result, unlike 1
. Dates were taken from the txt file.
from datetime import *
f = open('a0.txt')
n = f.readlines()
def dates(j):
l = n[j]
nl = list(l)
nl.remove('\n')
nl.remove('\t') #these were used because origianal txt file inlcuded tabulation
DD1 = int(nl[0] + nl[1])
MM1 = int(nl[3] + nl[4])
YY1 = int(nl[6] + nl[7] + nl[8] + nl[9])
DD2 = int(nl[10] + nl[10])
MM2 = int(nl[13] + nl[14])
YY2 = int(nl[16] + nl[17] + nl[18] + nl[19]) #day, month, year for each date
FD = date(YY1, MM1, DD1)
SD = date(YY2, MM2, DD2)
delta = abs(SD - FD) #difference
return delta
for i in range(len(n)): #difference for each line of dates
print(dates(i))
On that loop iteration, the code is trying to use 0 for the day:
date(2024,3,0)
This is because you have an incorrect calculation for the day:
DD2 = int(nl[10] + nl[10])
You're adding nl[10]
twice, but you meant to use nl[10] + nl[11]
.