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


2 thoughts on “Addition and subtraction of datetime with time in Python”

  1. Is there an easy button? I just want “end_time = start_time +1 minute” or something . Why does it take 15 lines of code and there are no good ways of testing except write in a script and then exist the script run the script the script fails so you have to edit the script again. Why aren’t codes easy? Why would anyone want to be a coder if you have to write 20 lines to do one thing?

    Reply
    • Hi Bob,
      This example is to add hours minutes and seconds separately to a daytime variable. However, if you need to add hours or minutes in a single line, then you can use timedelta with multiple arguments like timedelta(days=10, seconds=10, microseconds=10, milliseconds=10, minutes=10, hours=10, weeks=10) and add it to your start_time. Here is an example:

      from datetime import datetime, timedelta
      start_time = datetime(year=2006, month=11, day=23, hour=10, minute=10)
      end_time = start_time + timedelta(hours = 3, minutes=15, seconds=30)
      print(“After adding time: “, end_time, “\n”)

      Reply

Leave your thoughts...

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