Getting today's date in YYYY-MM-DD in Python?
Categories:
Getting Today's Date in YYYY-MM-DD Format in Python

Learn how to effortlessly retrieve and format the current date in Python using the datetime
module, ensuring a consistent YYYY-MM-DD output.
Working with dates is a common task in programming, and Python's datetime
module provides robust tools for handling dates and times. One of the most frequent requirements is to get the current date and format it into a standardized string, such as YYYY-MM-DD
. This article will guide you through the simple steps to achieve this, covering the essential functions and methods from the datetime
module.
Understanding the datetime
Module
The datetime
module in Python supplies classes for working with dates and times. The primary classes you'll interact with are date
, time
, datetime
, and timedelta
. For our purpose of getting today's date, the datetime
class is particularly useful as it combines both date and time information, and provides methods to extract just the date part or format the entire object.
flowchart TD A[Start] --> B{Import datetime module} B --> C[Get current datetime: datetime.now()] C --> D[Extract date part: .date()] D --> E[Format date: .strftime('%Y-%m-%d')] E --> F[End]
Flowchart illustrating the process of getting and formatting today's date.
Retrieving and Formatting Today's Date
To get today's date in the YYYY-MM-DD
format, we'll use the datetime.now()
method to get the current date and time, and then the strftime()
method to format it. The strftime()
method takes a format string as an argument, where %Y
represents the full year, %m
represents the month as a zero-padded decimal number, and %d
represents the day of the month as a zero-padded decimal number.
from datetime import datetime
# Get the current date and time
current_datetime = datetime.now()
# Format the date into YYYY-MM-DD
today_date_formatted = current_datetime.strftime('%Y-%m-%d')
print(f"Today's date: {today_date_formatted}")
Python code to get and format today's date.
datetime.now()
gives you the current date and time, if you only need the date part without any time information, you can use datetime.today().date()
or date.today()
for a slightly more direct approach to get a date
object first.Alternative: Using date.today()
If you are strictly interested in only the date and don't need the time component at all, the date.today()
method from the date
class is a more direct way to get a date
object representing the current local date. You can then apply strftime()
to this date
object.
from datetime import date
# Get today's date as a date object
today = date.today()
# Format the date into YYYY-MM-DD
today_date_formatted = today.strftime('%Y-%m-%d')
print(f"Today's date (using date.today()): {today_date_formatted}")
Python code using date.today()
to get and format the current date.
strftime()
method is incredibly versatile. You can customize the output format extensively using various format codes. For example, %A
for full weekday name, %B
for full month name, or %H:%M:%S
for time.