Python datetime objects require a year, however I would just like to write a function that outputs the differences in dates between two day-months ignoring year. Meaning the output range of this function is [-182, 183], since the calculations should "wrap around" the year.
Example: Goal date: 1st Jan Guess: 31st Dec Result: -1
Goal date: 1st Jan Guess: 2nd Jan Result: +1
def date_diff(goal, guess):
#goal and guess are in the format "%m-%d"
goal_date = datetime.strptime(goal + "-2020", "%m-%d-%Y")
goal_date1 = datetime.strptime(goal + "-2021", "%m-%d-%Y")
guess_date = datetime.strptime(guess + "-2020", "%m-%d-%Y")
guess_date1 = datetime.strptime(guess + "-2021", "%m-%d-%Y")
r1 = goal_date - guess_date
r1 = r1.days
r3 = goal_date1 - guess_date
r3 = r3.days
r2 = guess_date1 - goal_date
r2 = r2.days
r4 = guess_date - goal_date
r4 = r4.days
r = ((r1, math.copysign(1, r1)), (r2, math.copysign(1, r2)),(r3, math.copysign(1, r2)),(r4, math.copysign(1, r4)))
#idea here was the find min of index 0 of each tuple then restore the sign, but i think i'm missing some combinations
smallest = min(r, key = lambda x:abs(x[0]))
return smallest[0]*smallest[1]