How to Get Today’s Date in YYYY MM DD Format in Python


By squashlabs, Last Updated: November 2, 2023

How to Get Today’s Date in YYYY MM DD Format in Python

To get today’s date in the YYYY MM DD format in Python, you can use the datetime module. Here are two possible ways to achieve this:

Method 1: Using the strftime() Method

You can use the strftime() method from the datetime module to format the current date according to your desired format. The strftime() method allows you to specify a format string that defines how the date should be displayed.

Here’s an example that shows how to get today’s date in the YYYY MM DD format:

from datetime import datetime

# Get today's date
today =

# Format the date as YYYY MM DD
formatted_date = today.strftime("%Y %m %d")

# Print the formatted date

This will output the current date in the format “YYYY MM DD”, such as “2022 01 01”.

Related Article: How To Limit Floats To Two Decimal Points In Python

Method 2: Using the strptime() and strftime() Methods

Another approach is to use the strptime() method to parse the current date as a string and then use the strftime() method to format it.

Here’s an example that demonstrates this approach:

from datetime import datetime

# Get the current date as a string
current_date_str ="%Y-%m-%d")

# Parse the current date string
current_date = datetime.strptime(current_date_str, "%Y-%m-%d")

# Format the parsed date as YYYY MM DD
formatted_date = current_date.strftime("%Y %m %d")

# Print the formatted date

In this example, we first use strftime() to get the current date as a string in the format “YYYY-MM-DD”. Then, we use strptime() to parse the date string and convert it to a datetime object. Finally, we use strftime() again to format the datetime object as “YYYY MM DD”.

Best Practices and Suggestions

When working with dates in Python, it’s important to keep the following best practices in mind:

1. Import the datetime module: Before using any of the date-related functions and classes, make sure to import the datetime module.

2. Use the appropriate format codes: When formatting dates using the strftime() method, you need to use the appropriate format codes to represent different date components. For example, %Y represents the four-digit year, %m represents the zero-padded month, and %d represents the zero-padded day.

3. Be aware of the system’s default locale: The strftime() method can be affected by the system’s default locale settings. To ensure consistent date formatting, you can set the LC_TIME environment variable to a specific locale before running your Python script. For example, you can set it to “C” for the default POSIX locale.

4. Consider using third-party libraries: If you need to perform more advanced date manipulation or handle time zones, you might consider using third-party libraries like dateutil or pytz. These libraries provide additional functionality and make certain tasks easier.

Related Article: How To Rename A File With Python

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How To Check If List Is Empty In Python

Determining if a list is empty in Python can be achieved using simple code examples. Two common methods are using the len() function and the not operator. This article... read more

How To Check If a File Exists In Python

Checking if a file exists in Python is a common task for many developers. This article provides simple code snippets and explanations on how to check file existence... read more

How to Use Inline If Statements for Print in Python

A simple guide to using inline if statements for print in Python. Learn how to use multiple inline if statements, incorporate them with string formatting, and follow... read more

How to Use Stripchar on a String in Python

Learn how to use the stripchar function in Python to manipulate strings. This article covers various methods such as strip(), replace(), and regular expressions. Gain... read more

How To Delete A File Or Folder In Python

Deleting files or folders using Python is a common task in software development. In this article, we will guide you through the process step-by-step, using simple... read more

How To Move A File In Python

Learn how to move a file in Python with this simple guide. Python move file tutorial for beginners. This article discusses why the question of moving files in Python is... read more