Addition and subtraction of datetime with time in Python

Earlier we have seen adding and subtracting days to and from date object. Now we will see how to perform addition and subtraction of datetime with time object.

Adding hours or minutes or seconds

To start with, let us see how to add hours or minutes or seconds or microseconds individually to a datetime object. To do time additions, use the timedelta object’s arguments to add the individual time components and add the timedelta object with the date object. here is an example.

## Adding hours or minutes or seconds to datetime
from datetime import datetime, timedelta

## Original datetime
datetime_original = datetime(year=2006, month=11, day=23)
print("\nOriginal date: ", datetime_original, "\n")

## Adding Hours
hours_to_add = 12
datetime_new = datetime_original + timedelta(hours = hours_to_add)
print("After adding hours: ", datetime_new, "\n")

## Adding Minutes
minutes_to_add = 45
datetime_new = datetime_new + timedelta(minutes = minutes_to_add)
print("After adding minutes: ", datetime_new, "\n")

## Adding Seconds
seconds_to_add = 33
datetime_new = datetime_new + timedelta(seconds = seconds_to_add)
print("After adding seconds: ", datetime_new, "\n")

## Adding Microseconds
microseconds_to_add = 12345
datetime_new = datetime_new + timedelta(microseconds = microseconds_to_add)
print("After adding microseconds: ", datetime_new, "\n")
Adding hours or minutes or seconds to datetime

Subtracting hours or minutes or seconds

To perform subtraction of individual time components from datetme object, just create a timedelta object with the time component and then subtract from the datetime object. Here is an example.

## Subtracting hours or minutes or seconds to datetime
from datetime import datetime, timedelta

## Original datetime
datetime_original = datetime(year=2006, month=11, day=23)
print("\nOriginal date: ", datetime_original, "\n")

## Subtracting Hours
hours_to_add = 12
datetime_new = datetime_original - timedelta(hours = hours_to_add)
print("After subtracting hours: ", datetime_new, "\n")

## Subtracting Minutes
minutes_to_add = 45
datetime_new = datetime_new - timedelta(minutes = minutes_to_add)
print("After subtracting minutes: ", datetime_new, "\n")

## Subtracting Seconds
seconds_to_add = 33
datetime_new = datetime_new - timedelta(seconds = seconds_to_add)
print("After subtracting seconds: ", datetime_new, "\n")

## Subtracting Microseconds
microseconds_to_add = 12345
datetime_new = datetime_new - timedelta(microseconds = microseconds_to_add)
print("After subtracting microseconds: ", datetime_new, "\n")
Subtracting hours or minutes or seconds from datetime in Python

Adding and subtracting datetime object with time object

So far, we have seen how to add and subtract individual time component like hours or minutes or seconds from a datetime object. Now we will see hot to add or subtract a time object with all the time components with a datetime object. To do this, you need to create a timedelta object with all the time components using the arguments. Here is an example to add or subtract a time of “10:23:45.162342” hours from a datetime using timedelta object.

## Adding or subtracting datetime with time
from datetime import datetime, timedelta

# Original datetime
datetime_original = datetime(year=2006, month=11, day=23)
print("\nOriginal datetime: ", datetime_original, "\n")

# Time to add or subtract
time_delta = timedelta(hours=10, minutes=23, seconds=45, microseconds=162342)
print("Timedelta: ", time_delta, "\n")

# Add
datetime_new = datetime_original + time_delta
print("After adding time: ", datetime_new, "\n")

# Subtract
datetime_new = datetime_original - time_delta
print("After subtracting time: ", datetime_new, "\n")
Adding and subtracting datetime object with time object in Python

Reference

Related Articles & Tools

Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.