time data does not match format

前端 未结 6 1593
有刺的猬
有刺的猬 2020-12-02 14:24

I get the following error:

time data \'07/28/2014 18:54:55.099000\' does not match format \'%d/%m/%Y %H:%M:%S.%f\'

But I cannot see what pa

6条回答
  •  心在旅途
    2020-12-02 14:43

    I had the exact same error but with slightly different format and root-cause, and since this is the first Q&A that pops up when you search for "time data does not match format", I thought I'd leave the mistake I made for future viewers:

    My initial code:

    start = datetime.strptime('05-SEP-19 00.00.00.000 AM', '%d-%b-%y %I.%M.%S.%f %p')
    

    Where I used %I to parse the hours and %p to parse 'AM/PM'.

    The error:

    ValueError: time data '05-SEP-19 00.00.00.000000 AM' does not match format '%d-%b-%y %I.%M.%S.%f %p'

    I was going through the datetime docs and finally realized in 12-hour format %I, there is no 00... once I changed 00.00.00 to 12.00.00, the problem was resolved.

    So it's either 01-12 using %I with %p, or 00-23 using %H.

提交回复
热议问题