Python Date Time Addition and Subtraction with Pendulum

This Python Pendulum tutorial to show you how to add or subtract date and time value to a DateTime instance using Pendulum package. With different Python example program we will learn how to add or subtract years, months, days, hours, minutes and seconds value.

Table of contents

  1. Install Pendulum package
  2. Add days, months and years to DateTime instance
  3. Add hours, minutes and seconds to DateTime instance
  4. Subtract days, months and years from DateTime instance
  5. Subtract hours, minutes and seconds from DateTime instance
  6. Add and subtract with negative values

Install Pendulum package

Installing the Pendulum package using the below command.

pip install pendulum

Add days, months and years to DateTime instance

Following Python program to show you how to use add() method to add days, months or year to a DateTime instance which return a new instance of DateTime.

import pendulum

dt1 = pendulum.datetime(2021, 1, 1)
# add 3 days
dt2 = dt1.add(days=3)
# add 4 months
dt3 = dt1.add(months=4)
# add 5 years
dt4 = dt1.add(years=5)
# add 2 years, 3 months and 4 days
dt5 = dt1.add(years=2, months=3, days=4)

print(dt1.to_datetime_string())
print(dt2.to_datetime_string())
print(dt3.to_datetime_string())
print(dt4.to_datetime_string())
print(dt5.to_datetime_string())
The program output.
2021-01-01 00:00:00
2021-01-04 00:00:00
2021-05-01 00:00:00
2026-01-01 00:00:00
2023-04-05 00:00:00

Add hours, minutes and seconds to DateTime instance

We also user add() method to add seconds, minutes or hours as Python program below.

import pendulum

dt1 = pendulum.datetime(2021, 1, 1, 1, 0 , 0)
# add 5 hours
dt2 = dt1.add(hours=5)
# add 20 minutes
dt3 = dt1.add(minutes=20)
# add 5 seconds
dt4 = dt1.add(seconds=5)
# add 2 hours, 3 minutes and 4 seconds
dt5 = dt1.add(hours=2, minutes=3, seconds=4)
# add 1 year, 2 months, 3 days, 4 hours, 5 minutes and 6 seconds
dt6 = dt1.add(years=1, months=2, days=3, hours=4, minutes=5, seconds=6)

print(dt1.to_datetime_string())
print(dt2.to_datetime_string())
print(dt3.to_datetime_string())
print(dt4.to_datetime_string())
print(dt5.to_datetime_string())
print(dt6.to_datetime_string())
The program output.
2021-01-01 01:00:00
2021-01-01 06:00:00
2021-01-01 01:20:00
2021-01-01 01:00:05
2021-01-01 03:03:04
2022-03-04 05:05:06

Subtract days, months and years from DateTime instance

In the following Python program we use subtract() method to subtract days, months, years value from a DateTime instance and return new DateTime instance with subtracted values.

import pendulum

dt1 = pendulum.datetime(2021, 6, 20)
# subtract 3 days
dt2 = dt1.subtract(days=3)
# subtract 4 months
dt3 = dt1.subtract(months=4)
# subtract 5 years
dt4 = dt1.subtract(years=5)
# subtract 2 years, 3 months and 4 days
dt5 = dt1.subtract(years=2, months=3, days=4)

print(dt1.to_datetime_string())
print(dt2.to_datetime_string())
print(dt3.to_datetime_string())
print(dt4.to_datetime_string())
print(dt5.to_datetime_string())
The program output.
2021-06-20 00:00:00
2021-06-17 00:00:00
2021-02-20 00:00:00
2016-06-20 00:00:00
2019-03-16 00:00:00

Subtract hours, minutes and seconds from DateTime instance

We also can use subtract() method to subtract hours, minutes or seconds from DateTime instance.

import pendulum

dt1 = pendulum.datetime(2021, 8, 25, 15, 30 , 50)
# subtract 5 hours
dt2 = dt1.subtract(hours=5)
# subtract 20 minutes
dt3 = dt1.subtract(minutes=20)
# subtract 5 seconds
dt4 = dt1.subtract(seconds=5)
# subtract 2 hours, 3 minutes and 4 seconds
dt5 = dt1.subtract(hours=2, minutes=3, seconds=4)
# subtract 1 year, 2 months, 3 days, 4 hours, 5 minutes and 6 seconds
dt6 = dt1.subtract(years=1, months=2, days=3, hours=4, minutes=5, seconds=6)

print(dt1.to_datetime_string())
print(dt2.to_datetime_string())
print(dt3.to_datetime_string())
print(dt4.to_datetime_string())
print(dt5.to_datetime_string())
print(dt6.to_datetime_string())
The program output.
2021-08-25 15:30:50
2021-08-25 10:30:50
2021-08-25 15:10:50
2021-08-25 15:30:45
2021-08-25 13:27:46
2020-06-22 11:25:44

Add and subtract with negative values

If we add negative value, the add() method will act like the subtract() method.

import pendulum

dt1 = pendulum.datetime(2021, 2, 2)
dt2 = dt1.add(years=-1, months=-1, days=-1)

print(dt1.to_datetime_string())
print(dt2.to_datetime_string())
The program output.
2021-02-02 00:00:00
2020-01-01 00:00:00

If we subtract negative value, the subtract() method will act like the add() method.

import pendulum

dt1 = pendulum.datetime(2021, 2, 2)
dt2 = dt1.subtract(years=-1, months=-1, days=-1)

print(dt1.to_datetime_string())
print(dt2.to_datetime_string())
The program output.
2021-02-02 00:00:00
2022-03-03 00:00:00

Happy Coding 😊