0
import datetime
from datetime import timedelta

todays_date = datetime.datetime.today()

todays_date = todays_date.strftime('%Y-%m-%d')

goal:

I want to return: 2022-4-9 Currently returning: 2022-04-09

I tried doing this, but it gives me an Invalid format string error:

todays_date = todays_date.strftime('%Y-%-m-%-d')

The documentation that I looked at is located here:

https://www.programiz.com/python-programming/datetime/strftime

What am I doing wrong?

  • 1
    I'm guessing you're on Windows, so you'll want `today.strftime('%Y-%#m-%#d')` (note the `#` instead of `-`) – C.Nivs Apr 29 '22 at 17:27
  • 3
    For the record, the unofficial guides are frequently out of date and/or inaccurate. [Python's official `strftime` docs provide a useful table of features](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes), and the short version is, they're all zero-padded. As the duplicate notes, on certain platforms there are cheats around this; odds are the guide you looked at inadvertently documented platform-specific code. Personally, I'd recommend keeping the zeroes (which makes the output string sort by date), but the duplicate provides options if that's unacceptable. – ShadowRanger Apr 29 '22 at 17:30
  • I'm using Selenium to pull a report and the dates in the report URL have no padded zero, that's why I needed them off. – Chicken Sandwich No Pickles Apr 29 '22 at 17:32

0 Answers0