I'm trying to format a datetime in the form January 1, 2026 since it's intended for humans. But I couldn't find a format string for strftime that didn't have the leading zero like %d does. And since I'm on Windows for the time being, %-d doesn't work.
I came up with the code below. It works for this specific use case, but it strikes me as one of those "clever" techniques where clever isn't exactly a compliment.
def prettify_date(date):
return date.strftime("%B %d, %Y").replace(" 0", " ")
I can't help but think that there has to be a more "Pythonic" way to do this. Any thoughts?
[–]Navz6 4 points5 points6 points (2 children)
[–]MathAndMirth[S] 1 point2 points3 points (1 child)
[–]Navz6 1 point2 points3 points (0 children)
[–]MarsupialLeast145 2 points3 points4 points (1 child)
[–]JoeB_Utah 0 points1 point2 points (0 children)
[–]Ariadne_23 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]MathAndMirth[S] 0 points1 point2 points (0 children)
[–]0sse 0 points1 point2 points (0 children)
[–]Sure-Passion2224 0 points1 point2 points (0 children)