Table of Contents
- Date formats in Python
- How to convert date to YYYYMMDD format in Python
- Using datetime.strftime() Function to Format Date to YYYYMMDD in Python
- Using datetime.date Object to Format Date to YYYYMMDD in Python
- Using date.isoformat() Function to Format Date to YYYYMMDD in Python
- Using arrow.format() Function to Format Date to YYYYMMDD in Python
- Using pendulum.to_date_string() Function to Format Date to YYYYMMDD in Python
- Using pandas.strftime() Function to Format Date to YYYYMMDD in Python
- Using pandas.isoformat() Function to Format Date to YYYYMMDD in Python
- Get today’s date in YYYYMMDD format
- Conclusion
Date formats in Python
We can work with different modules to store a date in Python. Some of them are datetime
, arrow
, pendulum
, and more. The format can vary from module to module.
A date has three attributes year, month, and day. The order of these attributes determines the format of dates. The most basic format accepted everywhere is the YYYYMMDD. Even while creating date objects using the above-mentioned modules, we get the YYYYMMDD format.
However, sometimes the format may be different while creating date objects from strings in Python.
How to convert date to YYYYMMDD format in Python
This article will discuss how to format date to YYYYMMDD in Python.
Using datetime.strftime()
Function to Format Date to YYYYMMDD in Python
The strftime()
is used to convert a datetime
object to a string with a given format. We can specify the format for YYYY-MM-DD in the function as %Y-%m-%d
.
Note that this function returns the date as a string.
See the code below.
1 2 3 4 5 6 |
import datetime d = datetime.datetime.strptime("12/10/2020", "%d/%m/%Y") s = d.strftime('%Y%m%d') print(s) |
Output:
In the above example,
- The
strptime()
function creates adate
object using a string and a format. - The format specified is
%d/%m/%Y
. - We convert the format to YYYYMMDD using the
strftime()
function.
Using datetime.date
Object to Format Date to YYYYMMDD in Python
The datetime
module works with date and time values in Python. We can create objects of the datetime.date
class and by default, it will format the date to YYYYMMDD in Python.
For example,
1 2 3 4 5 |
import datetime d =datetime.date(2022,10,12) print(d) |
Output:
Using date.isoformat()
Function to Format Date to YYYYMMDD in Python
The ISO format follows the YYYY-MM-DD format for dates. We can convert date
objects to this format by using the isoformat()
function.
This function will also return the date as a string.
For example,
1 2 3 4 5 6 |
import datetime d = datetime.datetime.strptime("12/10/2020", "%d/%m/%Y") s = d.isoformat() print(s) |
Output:
Further reading:
Using arrow.format()
Function to Format Date to YYYYMMDD in Python
The arrow
module is gaining popularity for its simplicity while storing the date and time values. We can specify the format for dates by using the format()
function with the arrow
objects.
The YYYY-MM-DD
format can be mentioned in the format()
function.
See the code below.
1 2 3 4 5 6 |
import arrow d = arrow.get('12-10-2022', 'DD-MM-YYYY') s = d.format('YYYYMMDD') print(s) |
Output:
In the above example,
- We create a date from a string using the
arrow.get()
function. - The format for the date in this function is
DD-MM-YYYY
. - We convert the format to YYYYMMDD using the
format()
function.
Using pendulum.to_date_string()
Function to Format Date to YYYYMMDD in Python
The pendulum
module is based on the traditional datetime
package. It can handle the date and time objects.
The to_date_string()
function from this module is used to return the date as a string in the YYYY-MM-DD format.
We can format date to YYYYMMDD in Python using this function.
For example,
1 2 3 4 5 |
import pendulum d = pendulum.from_format('12-10-2022', 'DD-MM-YYYY') print(d.to_date_string()) |
Output:
In the above example,
- We create a date in the
DD-MM-YYYY
format using thefrom_format()
function. - This function creates a date object using the string and format provided as arguments.
- We format the date to YYYYMMDD in Python using the
to_date_string()
function. - The final result is a string.
Using pandas.strftime()
Function to Format Date to YYYYMMDD in Python
The pandas
module is used to create and manage DataFrames in Python. This module also has functionalities to create and handle Timestamp values since these values are a common occurrence in DataFrames.
We can use the strftime()
function with these pandas.Timestamp
objects as well. We will mention the same arguments as we did previously to format date to YYYYMMDD in Python using this method.
For example,
1 2 3 4 5 |
import pandas as pd d = pd.to_datetime('12-10-2022', format = '%d-%m-%Y') print(d.strftime('%Y%m%d')) |
Output:
Using pandas.isoformat()
Function to Format Date to YYYYMMDD in Python
We discussed the isoformat()
function earlier. This function can be similarly used with the pandas.Timestamp
objects to format date to YYYYMMDD in Python.
See the code below.
1 2 3 4 5 |
import pandas as pd d = pd.to_datetime('12-10-2022', format = '%d-%m-%Y') print(d.isoformat()) |
Output:
Get today’s date in YYYYMMDD format
You can use following code to get today’s date in YYYYMMDD format.
1 2 3 4 5 |
import time print time.strftime('%Y%m%d') |
Output:
Conclusion
This tutorial discussed how to format date to YYYYMMDD in Python. By default, most of the object constructors use the same format. However, other formats might be encountered in Python. We discussed the use of the strftime()
and isoformat()
function which are used for formatting datetime
objects. The use of these functions with the pandas
library was also discussed. Methods from other modules like the pendulum.to_date_string()
and arrow.format()
are also demonstrated.
That’s all about how to Format Date to YYYYMMDD in Python.