Why didn't the command dt.datetime.strptime result in 1984-12-24 00:00:00 instead of datetime.datetime(1984, 12, 24, 0, 0)?

Screen Link:
https://app.dataquest.io/m/403/working-with-dates-and-times-in-python/5/using-strptime-to-parse-strings-as-dates

My Code:

date_format = "%m/%d/%y %H:%M"

for row in potus:
    start_date = row[2]
    start_date = dt.datetime.strptime(start_date, date_format)
    row[2] = start_date

What I expected to happen:
date_1_str = “24/12/1984”
date_1_dt = dt.datetime.strptime(date_1_str, “%d/%m/%Y”)
print(type(date_1_dt))
print(date_1_dt)

class ‘datetime.datetime’
1984-12-24 00:00:00

What actually happened:

[['Joshua T. Blanton',
  '2014-12-18T00:00:00',
  datetime.datetime(2015, 1, 6, 9, 30),
  '1/6/15 23:59',
  '',
  'potus',
  'west wing',
  'JointService Military Honor Guard'],

dt.datetime.strptime() function return datetime object like. datetime.datetime(1984, 12, 24, 0, 0) that is why you have datetime.datetime(2015, 1, 6, 9, 30) in your list data.

but when you print datetime object it actually call built-in object.__str__() function which turn it into human readable format. like (1984-12-24 00:00:00)

Example:

date_1_str = "24/12/1984"
date_1_dt = dt.datetime.strptime(date_1_str, "%d/%m/%Y")

This is datetime object

print(type(date_1_dt))
<class 'datetime.datetime'>

When you print it it will print string format of object by calling internally __str__ function.

print(date_1_dt)                
1984-12-24 00:00:00

print(date_1_dt.__str__())       # equivalent to `print(date_1_dt)`
1984-12-24 00:00:00

Using repr you can see actual string representation of an object

print(repr(date_1_dt)) 
datetime.datetime(1984, 12, 24, 0, 0)
2 Likes

Thanks ! That was helpful.