-1

I want to add some days to a datetime in Python

slday = item.SlDay
traceOut(str(slday))

This is the original date being printed

9/10/2018 12:00:00 AM

I would like to add some days to this original date. I checked using timedelta but couldn´t make it

date_1 = datetime.datetime.strptime(str(slday), "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=5)

the log shows the following error:

time data does not match format data=9/10/2018 12:00:00 AM, fmt=%m/%d/%y, to: M'/'d'/'yy

Guillermo Zooby
  • 582
  • 2
  • 15
  • 32
  • 1
    [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Instead of giving us one output path to what appears to be correct data, give us the input (i.e. direct assignments) and code to elicit the error. Include the full error message -- "couldn't make it" is not a problem specification. – Prune Sep 13 '18 at 15:48
  • [Duplicate](https://stackoverflow.com/questions/441147/how-to-subtract-a-day-from-a-date) – Prune Sep 13 '18 at 15:51

1 Answers1

1

This might be because you are using "%m/%d/%y" instead of "%m/%d/%Y"

d = '9/10/2018'
date_1 = datetime.datetime.strptime(d, "%m/%d/%Y")

It works when you applied that small change

Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
  • 1
    Mmmm, but they're saying the date is printed as `9/10/2018 12:00:00 AM` so, if that's the case, their format string is well off, more than just `%y` – roganjosh Sep 13 '18 at 15:43
  • yes, i think it´s the format string that is making noise cause log says time data does not match format data=9/10/2018 12:00:00 AM, fmt=%m/%d/%y, to: M'/'d'/'yy – Guillermo Zooby Sep 13 '18 at 16:46