W3docs

Getting today's date in YYYY-MM-DD in Python?

You can use the datetime module in Python to get the current date in the YYYY-MM-DD format.

You can use the datetime module in Python to get the current date in the YYYY-MM-DD format. Here is a code snippet that demonstrates how to do this:

get the current date in the YYYY-MM-DD format in Python

from datetime import datetime

# get the current date
now = datetime.now()

# format the date as YYYY-MM-DD
date_string = now.strftime("%Y-%m-%d")

print(date_string)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

This code imports the datetime module, gets the current date and time using datetime.now(), and then formats the date as a string using the strftime() method. The %Y format code is used to display the year with century as a decimal number, %m for month and %d for day of the month. This will output the current date in the format of 'YYYY-MM-DD', for example '2022-01-19'