Working with Dates and Times in Python Using Pendulum

Introduction

In this article, we will learn about how to work with Dates and Times in Python Using the Pendulum library. Working with dates and times in programming can be a tricky task, especially when you have to consider different time zones, daylight saving times, and various formats. Python provides several built-in modules like datetime and time to work with dates and times, but they can be quite complex and lack some features. That's where the Pendulum library is useful. Pendulum is a Python library that makes working with dates and times more user-friendly while also providing additional functionality and features.

Installing Pendulum

pip install pendulum

Creating Dates and Times

With Pendulum, you can create dates and times using several different methods. Below are a few examples.

import pendulum

# Create a date from a string
date = pendulum.parse('2024-05-30')
print(date)  # Output: 2023-05-30T00:00:00+00:00

# Create a date and time from a string
datetime = pendulum.parse('2024-05-30 12:34:56')
print(datetime)  # Output: 2024-05-30T12:34:56+00:00

# Create a date from individual components
date = pendulum.date(2024, 5, 30)
print(date)  # Output: 2024-05-30T00:00:00+00:00

# Create a date and time from individual components
datetime = pendulum.datetime(2024, 5, 30, 12, 34, 56)
print(datetime)  # Output: 2024-05-30T12:34:56+00:00

# Create a date and time from a Unix timestamp
timestamp = 1717020082  
datetime = pendulum.from_timestamp(timestamp)
print(datetime)  # Output: 2024-05-30T12:34:56+00:00

Working with Time Zones

Pendulum makes it easy to work with different time zones. You can convert dates and times between time zones or create them directly in a specific time zone.

import pendulum

# Create a date and time in the Asia/Kolkata time zone
indian_datetime = pendulum.datetime(2024, 5, 30, 12, 34, 56, tz='Asia/Kolkata')
print(indian_datetime)  # Output: 2024-05-30T12:34:56+05:30

# Convert a date and time to UTC time zone
utc_datetime = indian_datetime.in_tz('UTC')
print(utc_datetime)  # Output: 2024-05-30T07:04:56+00:00

Date and Time Arithmetic Operations

Pendulum provides a convenient way to perform arithmetic operations on dates and times, such as adding or subtracting durations.

import pendulum

# Create a date and time
datetime = pendulum.datetime(2024, 5, 30, 12, 34, 56, tz='Asia/Kolkata')

# Add a duration
new_datetime = datetime.add(days=2, hours=3)
print(new_datetime)  # Output: 2024-06-01T15:34:56+05:30

# Subtract a duration
new_datetime = datetime.subtract(weeks=1, minutes=30)
print(new_datetime)  # Output: 2024-05-23T12:04:56+05:30

# Get the difference between two dates and times
diff = pendulum.datetime(2024, 6, 1, tz='Asia/Kolkata') - pendulum.datetime(2024, 5, 30, tz='Asia/Kolkata')
print(diff)  # Output: 2 days, 0:00:00

Date and Time Formatting and Parsing

import pendulum

# Create a date and time
datetime = pendulum.datetime(2024, 5, 30, 12, 34, 56, tz='Asia/Kolkata')

# Format a date and time
formatted_datetime = datetime.format('YYYY-MM-DD HH:mm:ss')
print(formatted_datetime)  # Output: 2024-05-30 12:34:56

# Parse a date and time from a string
parsed_datetime = pendulum.parse('2024/05/30 12:34:56', tz='Asia/Kolkata', strict=False)
print(parsed_datetime)  # Output: 2024-05-30T12:34:56+05:30

Summary

The Pendulum library provides a powerful and best way to work with dates and times in Python. Its features include creating and parsing dates and times, working with time zones, performing arithmetic operations, and formatting dates and times according to your needs. By using Pendulum, you can simplify date and time handling in your Python projects, making your code more readable and maintainable.


Similar Articles