When using Python strftime
, is there a way to remove the first 0 of the date if it's before the 10th, ie. so 01
is 1
? Can't find a %
thingy for that?
Thanks!
If you add a hyphen between the % and the letter, you can remove the leading zero. For example %Y/%-m/%-d. This only works on Unix (Linux, OS X), not Windows. On Windows, you would use #, e.g. %Y/%#m/%#d.
strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.
To convert Python datetime to string, use the strftime() function. The strftime() method is a built-in Python method that returns the string representing date and time using date, time, or datetime object.
Actually I had the same problem and I realized that, if you add a hyphen between the %
and the letter, you can remove the leading zero.
For example %Y/%-m/%-d
.
This only works on Unix (Linux, OS X), not Windows (including Cygwin). On Windows, you would use #
, e.g. %Y/%#m/%#d
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With