Python’s pendulum module
enables date/time conversions and manipulations.
It helps the user to work easily with date/time formats.
This module enables and provides all the functionalities provided by the pytz module.
Installation of pendulum module through the command-line:

Importing pendulum module:
import pendulum
Display the current time
The now()
method is used to display the current date-time of a particular zone.
Example:
import pendulum
time = pendulum.now()
print(time)
Output:
2020-01-03T10:46:59.049040+05:30
The timezone objects
along with the datetime.now()
function is used to avail the current timestamp of different time-zones.
Example:
from datetime import datetime
import pendulum
utc_time = pendulum.timezone('UTC')
pst_time = pendulum.timezone('America/Los_Angeles')
ist_time = pendulum.timezone('Asia/Calcutta')
print('Date Time in UTC =', datetime.now(utc_time))
print('Date Time in PST =', datetime.now(pst_time))
print('Date Time in IST =', datetime.now(ist_time))
Output:

Replacing the datetime module by pendulum module
import pendulum
utc = pendulum.now('UTC')
print('Date Time in UTC =', utc)
Output:
Date Time in UTC = 2020-01-03T05:28:43.853647+00:00
Conversion of time-zones
import pendulum
utc = pendulum.now('UTC')
ist = utc.in_timezone('Asia/Calcutta')
print('Date Time in IST =', ist)
Output:
Date Time in IST = 2020-01-03T11:05:20.756743+05:30
Date-time manipulations
The pendulum module provides add()
and subtract()
functions to manipulate the date and times in terms of year/month/hour and hour/minute/second.
import pendulum
d_t = pendulum.datetime(2020, 2, 29)
d_t.to_datetime_string()
print(d_t)
dt_add = d_t.add(years=5)
print(dt_add)
dt_add = d_t.add(months=5)
print(dt_add)
dt_add = d_t.add(days=2)
print(dt_add)
dt_add = d_t.add(weeks=5)
print(dt_add)
dt_add = d_t.add(hours=5)
print(dt_add)
dt_add = d_t.add(minutes=5)
print(dt_add)
dt_add = d_t.add(seconds=5)
print(dt_add)
dt_sub = d_t.subtract(years=1)
print(dt_sub)
dt_sub = d_t.subtract(months=5)
print(dt_sub)
dt_sub = d_t.subtract(days=2)
print(dt_sub)
dt_sub = d_t.subtract(weeks=5)
print(dt_sub)
dt_sub = d_t.subtract(hours=5)
print(dt_sub)
dt_sub = d_t.subtract(minutes=5)
print(dt_sub)
dt_sub = d_t.subtract(seconds=5)
print(dt_sub)
Output:

delta() function
The delta()
function provides the difference between the two timestamps.
import pendulum
d_t1 = pendulum.datetime(2020, 2, 20)
d_t1.to_datetime_string()
print(d_t1)
d_t2 = pendulum.datetime(2020, 2, 10)
d_t2.to_datetime_string()
print(d_t2)
delta = d_t1 - d_t2
print(delta.start)
print(delta.end)
print(delta.in_days()) # 10
print(delta.in_hours()) # 240
Output:

Formatting date-time
The strftime()
function enables the user to format the date-time in our own format.
import pendulum
utc = pendulum.now('UTC')
print(utc .to_iso8601_string())
print(utc .to_formatted_date_string())
print(utc .to_w3c_string())
print(utc .to_date_string())
# supports strftime() too
print(utc .strftime('%Y-%m-%d %H:%M:%S %Z%z'))
Output:
2020-01-03T07:17:28.650601Z
Jan 03, 2020
2020-01-03T07:17:28+00:00
2020-01-03
2020-01-03 07:17:28 UTC+0000
Comparison of Dates
Simple comparison of time-zones
is offered by the pendulum module.
import pendulum
first = pendulum.datetime(2012, 9, 5, 23, 26, 11, 0, tz='America/Toronto')
second = pendulum.datetime(2019, 9, 5, 20, 26, 11, 0, tz='America/Vancouver')
first.to_datetime_string()
print(first.timezone_name)
second.to_datetime_string()
print(second.timezone_name)
print(first == second)
print(first != second)
Output:
America/Toronto
America/Vancouver
False
True
Conclusion
Thus, in this article, we have understood the functionalities offered by the pendulum module.
References
- Python pendulum module
- Pendulum Documentation