close

[Solved] Python Datetime Strptime Error: ‘-‘ is a bad directive in format ‘%-m-%-d-%y %-H:%M:%S’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python Datetime Strptime Error: ‘-‘ is a bad directive in format ‘%-m-%-d-%y %-H:%M:%S’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python Datetime Strptime Error: ‘-‘ is a bad directive in format ‘%-m-%-d-%y %-H:%M:%S’ Error Occurs?

Today I get the following error Python Datetime Strptime Error: ‘-‘ is a bad directive in format ‘%-m-%-d-%y %-H:%M:%S’ in python.

How To Solve Python Datetime Strptime Error: ‘-‘ is a bad directive in format ‘%-m-%-d-%y %-H:%M:%S’ Error ?

  1. How To Solve Python Datetime Strptime Error: '-' is a bad directive in format '%-m-%-d-%y %-H:%M:%S' Error ?

    To Solve Python Datetime Strptime Error: '-' is a bad directive in format '%-m-%-d-%y %-H:%M:%S' Error The Python datetime module documentation explicitly specifies the format strings it supports, and this extension is not given.

  2. Python Datetime Strptime Error: '-' is a bad directive in format '%-m-%-d-%y %-H:%M:%S'

    To Solve Python Datetime Strptime Error: '-' is a bad directive in format '%-m-%-d-%y %-H:%M:%S' Error The Python datetime module documentation explicitly specifies the format strings it supports, and this extension is not given.

Solution 1

%-* — to skip padding — is a GNU libc extension. It’s not part of POSIX strftime, and thus not guaranteed to be portable to systems where your time-formatting calls aren’t eventually backed by GNU’s strftime C library function.

The Python datetime module documentation explicitly specifies the format strings it supports, and this extension is not given. Thus, while this is supported in GNU date and GNU strftime(), it isn’t available in Python datetime.

Solution 2

I had the same issue; date: 1/9/21

according to: https://strftime.org/ the correct format would’ve been “%-d/%-m/%y” which gave the bad directive error. “%d-/%m-/%y” didn’t work either.

Weirdly enough what worked was “%d/%m/%y”.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read