How can I get today’s date in Python in the YYYY-MM-DD format?
Is there a cleaner or more efficient way than using the following code?
str(datetime.datetime.today()).split()[0]
How can I get today’s date in Python in the YYYY-MM-DD format?
Is there a cleaner or more efficient way than using the following code?
str(datetime.datetime.today()).split()[0]
Hi Sakshi,
You can use the strftime method to get today’s date in the YYYY-MM-DD format:
from datetime import datetime
print(datetime.today().strftime('%Y-%m-%d'))
Output:
2021-01-26
If you only need the date (without time), you can directly use date.today:
from datetime import date
print(date.today().isoformat())
Output: 2021-01-26