Datetime and Conversion

Trying to go from creating a datetime object from a string all the way through to using .time() to just pull the time.
Can you help with the error please? Thanks!

test_datetime = "1/8/1990 19:32:05"
td_time = dt.strptime(test_datetime, "%M/%d/%Y %H:%m:%S")
td_time = td_time.time()

I am getting this error message:

Traceback (most recent call last):
  File "/root", line 83, in <module>
    td_time = dt.strptime(test_datetime, "%M/%d/%Y %H:%m:%S")
  File "root", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "root", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '1/8/1990 19:32:05' does not match format '%M/%d/%Y %H:%m:%S'

Hi @eugeniosp3
I tested this code and it work

test_datetime = "01/08/1990 19:32:05"
td_time = dt.datetime.strptime(test_datetime, "%m/%d/%Y %H:%M:%S")
td_time = td_time.time()


I did just small modification to test_datetime value because the Day and the Month must be a zero-padded decimal number (01,02,…)

1 Like

Thanks. My error was in the formatting as I had:
β€œ%M/%d/%Y %H:%m:%S”

and you have:
β€œβ€%m/%d/%Y %H:%M:%S""

%m =month as zero padded decimal number
%M = minute as zero padded decimal number

I was telling it to put minute in a month spot. (

Thank you!

You’re most welcome!

1 Like