Home > Software design >  Python: time data does not match format
Python: time data does not match format

Time:10-23

When I run the code, I get this error:

ValueError: time data '2021-10-26T10:15:00.000 04:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

The format seems to be correct, what's the problem? Does anyone had the same problem?

I'm using this method

# Convert 2019-09-03T12:31:03.806 0400 to 2019-09-13 16:30:12
def time_converter(time):
    if time is None:
        time = ''
    else:
        time_obj = datetime.strptime(time, '%Y-%m-%dT%H:%M:%S.%f%z')
        time = time_obj.strftime('%Y-%m-%d %H:%M:%S')
    return time

I just give it time in this format 2021-10-26T10:15:00.000 04:00

CodePudding user response:

You code worked for me without error, but in your Python version you may need to replace ':' with '' in time zone:

2021-10-26T10:15:00.000 04:00 2019-09-03T12:31:03.806 0400

CodePudding user response:

Problem was With Python Version. Remote Server Where the code was running had Python 3.6. As tromgy commented here, %z directive was added in 3.7

  • Related