12

I am trying to generate a random date and the add some days to that date, but I am facing this error. I guess it has to do with the format of my date but I can't find the solution.

I thought it was because I needed double digit numbers for the day and month, here is my code which produces an error.

start_day = randint(1,31)
strt_day = []
strt_day.append("%02d" % start_day)
start_day = strt_day 

strt_moth = []
start_month = randint(2,4)
strt_moth.append("%02d" % start_month)
start_month = strt_moth

start_date = ""+start_month[0]+"/"+start_day[0]+"/2015"
depart = datetime.datetime.strptime(start_date, "%m/%d/%y")

Any idea on what I am doing wrong?

Thanks

JordanBelf
  • 3,208
  • 9
  • 47
  • 80
  • 2
    Because `%y` is for a **two-digit year**, e.g. `20` -> `2020`; `%Y` (note case!) is four digits. But why don't you just use `datetime.datetime(2015, randint(2, 4), randint(1, 31))` (note that not all months February-April have 31 days...)? – jonrsharpe Feb 11 '15 at 14:16
  • 1
    Confusingly naming your variables as typos of each other? – tripleee Feb 11 '15 at 14:16
  • @jonrsharpe thanks! yes, you are right, all that code you see was just me trying to find the error. – JordanBelf Feb 11 '15 at 14:18
  • @JordanBelf my point is that you don't need `strptime` at all – jonrsharpe Feb 11 '15 at 14:20
  • @jonrsharpe Thanks, I was learning from this example http://stackoverflow.com/questions/6871016/adding-5-days-to-date-in-python . Looks you are right though. – JordanBelf Feb 11 '15 at 14:22
  • @JordanBelf ah, I see - `strptime` is only needed there because they start with a string – jonrsharpe Feb 11 '15 at 14:22

1 Answers1

22

Because %y is for a two-digit year, so 2015 is interpreted as 20 (i.e. 2020) and the 15 is left over:

>>> import datetime
>>> datetime.datetime.strptime("01/02/20", "%d/%m/%y")
datetime.datetime(2020, 2, 1, 0, 0)
>>> datetime.datetime.strptime("01/02/2015", "%d/%m/%y")

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    datetime.datetime.strptime("01/02/2015", "%d/%m/%y")
  File "C:\Python27\lib\_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: 15

You want %Y (note case!), which is a four-digit year:

>>> datetime.datetime.strptime("01/02/2015", "%d/%m/%Y")
datetime.datetime(2015, 2, 1, 0, 0)

You should read through the docs, which explain the various format directives.


However, the extra steps involving strings seem pointless, why not just pass the integers you create to datetime.datetime?

>>> import random
>>> random.seed(0)
>>> datetime.datetime(2015, random.randint(2, 4), random.randint(1, 31))
datetime.datetime(2015, 4, 24, 0, 0)

Note that this might generate invalid dates (e.g. February doesn't have a 30th!):

>>> random.seed(8)
>>> datetime.datetime(2015, random.randint(2, 4), random.randint(1, 31))

Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    datetime.datetime(2015, random.randint(2, 4), random.randint(1, 31))
ValueError: day is out of range for month
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437