0

I have a date in string format. I'd like to calculate another date that is 1 year earlier. I tried to format the returned result but didn't get the expected format.

from dateutil.relativedelta import *
import datetime
latest = '2021-03-01'
date_minus_1y = datetime.datetime.strptime(latest, "%Y-%m-%d") - (dateutil.relativedelta.relativedelta(months=12))
date_minus_1y.strftime("%Y-%m%-%d")

I received result '2020-03%d' but I want 2020-03-01.

Thank you

Osca
  • 1,588
  • 2
  • 20
  • 41

1 Answers1

1
date_minus_1y.strftime("%Y-%m%-%d")

should be

date_minus_1y.strftime("%Y-%m-%d")
DevLounge
  • 8,313
  • 3
  • 31
  • 44