Python DateTime Formatting with Pendulum
Below, are examples of Python DateTime Formatting with Pendulum in Python:
- Using to_date_string()
- Using to_formatted_date_string()
- Using to_time_string()
- Using to_datetime_string()
- Using to_day_datetime_string()
Install Pendulum
First, we need to install Pendulum to perform Python DateTime Formatting. Install the Pendulum using the below command.
pip install pendulum
Python DateTime Formatting Using to_date_string() Method
In this example, below code uses the Pendulum library to generate a current date and time object (dt
) and then converts it to a date string (date_string
) representing only the date portion. Finally, it prints the date string.
import pendulum
# Create a Pendulum DateTime object
dt = pendulum.now()
# Convert to date string
date_string = dt.to_date_string()
print("Date String:", date_string)
Output
Date String: 2024-04-17
Python DateTime Formatting Using to_formatted_date_string() Method
In this example, below code uses Pendulum to create a current date and time object (dt
), and then it formats it into a custom date string (custom_date_string
) with the format “Day of the week, Month Day Year”, for example, “Sunday, January 1st 2024”.
import pendulum
# Create a Pendulum DateTime object
dt = pendulum.now()
# Convert to custom formatted date string
custom_date_string = dt.format("dddd, MMMM Do YYYY")
print("Custom Formatted Date String:", custom_date_string)
Output
Custom Formatted Date String: Wednesday, April 17th 2024
Python DateTime Formatting Using to_time_string() Method
In this example, below code employs Pendulum to create a current date and time object (dt
) and then converts it into a time string (time_string
). Finally, it prints the time string, representing the current time in the format “HH:MM:SS” (hour:minute:second).
import pendulum
# Create a Pendulum DateTime object
dt = pendulum.now()
# Convert to time string
time_string = dt.to_time_string()
print("Time String:", time_string)
Output
Time String: 05:32:46
Python DateTime Formatting Using to_datetime_string() Method
In this example, below code uses Pendulum to generate a current date and time object (dt
), which is then converted into a datetime string (datetime_string
) representing both the date and time.
import pendulum
# Create a Pendulum DateTime object
dt = pendulum.now()
# Convert to datetime string
datetime_string = dt.to_datetime_string()
print("Datetime String:", datetime_string)
Output
Datetime String: 2024-04-17 05:33:03
Python DateTime Formatting Using to_day_datetime_string() Method
In this example, below code uses Pendulum to create a current date and time object (dt
), which is then formatted into a datetime string (day_datetime_string
) with the format “Day of the week, Month Day Year Hour:Minute:Second”.
import pendulum
# Create a Pendulum DateTime object
dt = pendulum.now()
# Convert to datetime string with day
day_datetime_string = dt.format('dddd, MMMM DD YYYY HH:mm:ss')
print("Datetime String with Day:", day_datetime_string)
Output
Datetime String with Day: Wednesday, April 17 2024 05:34:04
Python DateTime Formatting with Pendulum
Effective date and time management is essential in many applications. Compared to the default datetime module, Pendulum is a Python library that offers a more user-friendly and intuitive interface for manipulating dates. In this article, we will learn about datetime formatting with Pendulum in Python.