Python Pendulum Module With Examples

Python Pendulum module is a drop-in replacement for the built-in datetime module. Python pendulum module supports timezones and provides useful methods to format, parse and date time manipulations. This module provides all the features of pytz module and much more.

Python Pendulum Module

We can install pendulum module using PIP command.

Python Pendulum Example

We can use the pendulum module to create timezone objects and use it with datetime.now() function to create timezone aware datetime instance.

Output:

Let’s see how to use pendulum module as a replacement of datetime module. However, if you are already using datetime module then it’s better to not mix them up.

Output:

Converting Timezones

Output:

Date Time Manipulations

We can use add() and subtract() functions for date time manipulations.

Output: Updated UTC Time 2018-09-25T09:16:45.031608+00:00

Date Time Formatting

There are some useful methods to convert date time to standard formatted string. Pendulum module also has strftime() function where we can specify our own format.

Output:

Parse String to Date Time

We can use parse() function to parse a string having commonly used formats to datetime object. If you want to specify format string, then use from_format() function.

Output:

Duration – timedelta replacement

Output:

Period of Time

Output:

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Website

By admin

Leave a Reply