Pendulum is a Python library for working with dates, times, and timezones. It provides a simple and intuitive API for parsing, manipulating, and formatting dates and times. In this article, we'll explore how to use Pendulum to work with time.
Installation To use Pendulum, you first need to install it. You can do this using pip, the Python package manager, by running the following command:
|
pip install pendulum
|
|
import pendulum
dt = pendulum.parse('2022-03-08 10:30:00')
print(dt)
|
Output:
| 2022-03-08T10:30:00+00:00 |
|
import pendulum
dt = pendulum.parse('2022-03-08 10:30:00', tz='UTC')
print(dt)
# Add 2 hours
dt = dt.add(hours=2)
print(dt)
# Subtract 30 minutes
dt = dt.subtract(minutes=30)
print(dt)
# Change timezone to New York
dt = dt.in_timezone('America/New_York')
print(dt)
|
Output:
|
2022-03-08T10:30:00+00:00 2022-03-08T12:30:00+00:00 2022-03-08T12:00:00+00:00 2022-03-08T07:00:00-05:00 |
|
import pendulum
dt = pendulum.parse('2022-03-08 10:30:00')
print(dt.format('MMMM Do YYYY, h:mm:ss A'))
|
Output:
| March 8th 2022, 10:30:00 AM |