0

I am trying to convert my time data from MM/DD/YYYY to M/D/YYYY in Python. I tried the line below but it does not work.

time_data_in_M_D_YYYY = datetime.strptime(time_data_in_MM_DD_YYYY, "%m-%d-%Y").strftime("%-m/%-d/%Y")

ValueError: Invalid format string

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Skylimit
  • 33
  • 6
  • Yes, `"%-m/%-d/%Y"` is an invalid format string. What you expect this to mean? Did you mean `"%m/%d/%Y"`? – mkrieger1 Oct 27 '21 at 22:26
  • `time_data in YYYY_DD_YYYY` either isn't valid Python, or would result in a `bool` if `time_data` and `YYYY_DD_YYYY` wer actually defined. Please provide an example that demonstrates the problem. – Grismar Oct 27 '21 at 22:26
  • @mkrieger1 why is it invalid? – Chris Oct 27 '21 at 22:27
  • @Chris I suppose `%-m` and `%-d` are not valid format specifiers. – mkrieger1 Oct 27 '21 at 22:28
  • I believe the are on UNIX systems – Chris Oct 27 '21 at 22:28
  • https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes doesn't list them. – mkrieger1 Oct 27 '21 at 22:29
  • @mkrieger1 the single digit format is not listed. what would be the best way to convert the format then? – Skylimit Oct 27 '21 at 22:30
  • @mkrieger1 I think it's calling the C strftime() on the backend, and the docs are showing universally supported directives, but I'm pretty sure on unix systems the non-zero padded formatting is supported - not that it would help OP's question. – Chris Oct 27 '21 at 22:32
  • Thanks. Since I have Windows, I used # before d and m, and it worked – Skylimit Oct 27 '21 at 22:39
  • Your example code works on my system without any erros (Python3.8 running on MacOS). Which version of Python and OS are you using? – Pi Marillion Oct 27 '21 at 22:47

0 Answers0