Skip to content

Instantly share code, notes, and snippets.

@ology
Last active September 14, 2025 22:47
Show Gist options
  • Save ology/c8c0f35cef8968f5d2ba8be0eb05d522 to your computer and use it in GitHub Desktop.
Save ology/c8c0f35cef8968f5d2ba8be0eb05d522 to your computer and use it in GitHub Desktop.
timedelta
from datetime import datetime, time, date, timedelta
today = date.today()
print(today) # 2025-09-14
my_time = time(10, 30, 0)
print(my_time) # 10:30:00
my_datetime = datetime.combine(today, my_time)
print(my_datetime) # 2025-09-14 10:30:00
duration_to_subtract = timedelta(minutes=30)
print(duration_to_subtract) # 0:30:00
new_datetime = my_datetime - duration_to_subtract
print(new_datetime) # 2025-09-14 10:00:00
new_time = new_datetime.time()
print(new_time_subtract) # 10:00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment