I'm using dateparser to parse strings and return a date value to compare against an extraction date then return an integer value as the difference between the two. In some cases, the parsed date is a range, in which case I average the two ends of the range and get the difference between the average and the extraction date.
I'm having trouble replacing the year as 2021 when the month rolls into next year, so my differences are all negative. How can I replace the year with 2021?
Example Output:
extraction date: 10/26/2020 0:00; promo_message: ["Arrives: Feb 26 - March 2"] ; first_delivery: -243 ; last_delivery: -238 ; delivery: -240
Code:
elif 'Arrives' in row[6] and '-' in row[6]:
try:
example_2 = promo_message.split('-',1)
first_delivery = (parse(example_2[0], fuzzy=True))
if first_delivery.month == 1:
first_delivery = first_delivery.replace(month=1, year=2021, tzinfo=None)
elif first_delivery.month == 2:
first_delivery = first_delivery.replace(month=2, year=2021, tzinfo=None)
elif first_delivery.month == 3:
first_delivery = first_delivery.replace(month=3, year=2021, tzinfo=None)
first_delivery = (parse(example_2[0], fuzzy=True))
first_delivery_int = (first_delivery - extraction_date).days
second_delivery = (parse(example_2[1], fuzzy=True))
if second_delivery.month == 1:
second_delivery = second_delivery.replace(month=1, year=2021)
elif second_delivery.month == 2:
second_delivery = second_delivery.replace(month=2, year=2021)
elif second_delivery.month == 3:
second_delivery = second_delivery.replace(month=3, year=2021)
second_delivery = (parse(example_2[1], fuzzy=True))
second_delivery_int = (second_delivery - extraction_date).days