Python Date Time Formatting with Pendulum

This Python Pendulum tutorial to show you how to format a DateTime instance as a string using Pendulum package. We will learn to use difference common methods to format string as well as using formatter method format() and strftime().

Table of contents

  1. Install Pendulum package
  2. Format DateTime string using common format methods
  3. Format DateTime string using formatter format() and strftime() method

Install Pendulum package

Installing the Pendulum package using the below command.

pip install pendulum

Format DateTime string using common format methods

The following methods to format a DateTime instance as a string.

  • to_date_string() to format the DateTime instance as date.
  • to_formatted_date_string() to format the DateTime instance as readable date.
  • to_time_string() to format the DateTime instance as time.
  • to_datetime_string() to format the DateTime instance as date and time.
  • to_day_datetime_string() to format the DateTime instance as day, date and time in English.
  • to_iso8601_string() to format the DateTime instance as ISO 8601 format.
  • to_w3c_string() to format the DateTime instance as W3C format.
  • to_atom_string() to format the DateTime instance as ATOM format.
  • to_cookie_string() to format the DateTime instance as cookie format.
  • to_rss_string() to format the DateTime instance as RSS format.
  • to_rfc1036_string() to format the DateTime instance as RFC 1036 format.
  • to_rfc1123_string() to format the DateTime instance as RFC 1123 format.
  • to_rfc2822_string() to format the DateTime instance as RFC 2822 format.
  • to_rfc3339_string() to format the DateTime instance as RFC 3339 format.
  • to_rfc822_string() to format the DateTime instance as RFC 822 format.
  • to_rfc850_string() to format the DateTime instance as RFC 850 format.

import pendulum

dt = pendulum.datetime(year=2021, month=1, day=1, hour=8, minute=20, second=30)

print('to_date_string() result:')
print(dt.to_date_string())

print('\nto_formatted_date_string() result:')
print(dt.to_formatted_date_string())

print('\nto_time_string() result: ')
print(dt.to_time_string())

print('\nto_datetime_string() result:')
print(dt.to_datetime_string())

print('\nto_day_datetime_string() result:')
print(dt.to_day_datetime_string())

print('\nto_iso8601_string() result:')
print(dt.to_iso8601_string())

print('\nto_w3c_string() result:')
print(dt.to_w3c_string())

print('\nto_atom_string() result:')
print(dt.to_atom_string())

print('\nto_cookie_string() result:')
print(dt.to_cookie_string())

print('\nto_rss_string() result: ')
print(dt.to_rss_string())

print('\nto_rfc1036_string() result:')
print(dt.to_rfc1036_string())

print('\nto_rfc1123_string() result:')
print(dt.to_rfc1123_string())

print('\nto_rfc2822_string() result:')
print(dt.to_rfc2822_string())

print('\nto_rfc3339_string() result:')
print(dt.to_rfc3339_string())

print('\nto_rfc822_string() result:')
print(dt.to_rfc822_string())

print('\nto_rfc850_string() result:')
print(dt.to_rfc850_string())
The program output.
to_date_string() result:
2021-01-01

to_formatted_date_string() result:
Jan 01, 2021

to_time_string() result:
08:20:30

to_datetime_string() result:
2021-01-01 08:20:30

to_day_datetime_string() result:
Fri, Jan 1, 2021 8:20 AM

to_iso8601_string() result:
2021-01-01T08:20:30Z

to_w3c_string() result:
2021-01-01T08:20:30+00:00

to_atom_string() result:
2021-01-01T08:20:30+00:00

to_cookie_string() result:
Friday, 01-Jan-2021 08:20:30 UTC

to_rss_string() result:
Fri, 01 Jan 2021 08:20:30 +0000

to_rfc1036_string() result:
Fri, 01 Jan 21 08:20:30 +0000

to_rfc1123_string() result:
Fri, 01 Jan 2021 08:20:30 +0000

to_rfc2822_string() result:
Fri, 01 Jan 2021 08:20:30 +0000

to_rfc3339_string() result:
2021-01-01T08:20:30+00:00

to_rfc822_string() result:
Fri, 01 Jan 21 08:20:30 +0000

to_rfc850_string() result:
Friday, 01-Jan-21 08:20:30 UTC

Format DateTime string using formatter format() and strftime() method

We also can use format() and strftime() methods to format string as below.

import pendulum

dt = pendulum.datetime(year=2021, month=1, day=1, hour=8, minute=20, second=30)

print(dt.format('dddd Do [of] MMMM YYYY HH:mm:ss A'))
print(dt.format('YYYY-MM-DD HH:mm:ss'))
print(dt.format('[Today is] dddd'))

print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print(dt.strftime('%I:%M:%S %p'))
The program output.
Friday 1st of January 2021 08:20:30 AM
2021-01-01 08:20:30
Today is Friday
2021-01-01 08:20:30 UTC+0000
08:20:30 AM

Happy Coding 😊