I've come across several answers (1, 2, 3) regarding the removal of leading zeros on python3
datetime
objects.
One of the most voted answers states:
On Windows, you would use #, e.g.
%Y/%#m/%#d
The code above doesn't work for me. I've also tried the Linux solution, which uses -
instead of #
, without success.
Code:
loop_date = "1950-1-1"
date_obj = datetime.strptime(loop_date, '%Y-%m-%d') # or '%Y-%#m-%#d' which produces the errors below
date_obj += timedelta(days=1)
print(date_obj)
# This the prints `1954-01-02` but I need `1954-1-2`
Traceback:
Traceback (most recent call last):
File "C:/collect_games.py", line 84, in <module>
date_obj = datetime.strptime(loop_date, '%Y-%-m-%d')
File "E:\Anaconda3\lib\_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "E:\Anaconda3\lib\_strptime.py", line 354, in _strptime
(bad_directive, format)) from None
ValueError: '#' is a bad directive in format '%Y-%#m#%d'
What's most pythonic approach to this problem?