Date arithmetic helps us add or subtract time from dates. It makes working with dates easy, like finding how many days between two dates or adding days to a date.
0
0
Date arithmetic (Timedelta) in Data Analysis Python
Introduction
Calculating the number of days between two events, like project start and end dates.
Adding a certain number of days or hours to a date, like setting a deadline 7 days from today.
Finding the difference in time between two timestamps, such as login and logout times.
Adjusting dates for scheduling, like moving an appointment 3 days later.
Syntax
Data Analysis Python
from datetime import datetime, timedelta # Create a timedelta object delta = timedelta(days=5, hours=3) # Add timedelta to a date new_date = datetime.now() + delta # Subtract timedelta from a date old_date = datetime.now() - delta
timedelta represents a duration, like days, seconds, or hours.
You can add or subtract timedelta objects to datetime objects to get new dates.
Examples
This adds 10 days to today's date and prints the new date.
Data Analysis Python
from datetime import datetime, timedelta # Add 10 days to current date today = datetime.now() future = today + timedelta(days=10) print(future)
This calculates how many days are between June 1 and June 10, 2024.
Data Analysis Python
from datetime import datetime, timedelta # Find difference between two dates d1 = datetime(2024, 6, 1) d2 = datetime(2024, 6, 10) diff = d2 - d1 print(diff.days)
This subtracts 2 hours from the current time and prints the result.
Data Analysis Python
from datetime import datetime, timedelta # Subtract 2 hours from current time now = datetime.now() past = now - timedelta(hours=2) print(past)
Sample Program
This program shows how to add and subtract time from the current date and how to find the number of days between two fixed dates.
Data Analysis Python
from datetime import datetime, timedelta # Current date and time now = datetime.now() print(f"Now: {now}") # Create a timedelta of 7 days and 5 hours delta = timedelta(days=7, hours=5) # Add timedelta to current date future_date = now + delta print(f"Future date (7 days, 5 hours later): {future_date}") # Subtract timedelta from current date past_date = now - delta print(f"Past date (7 days, 5 hours earlier): {past_date}") # Calculate difference between two dates start_date = datetime(2024, 6, 1) end_date = datetime(2024, 6, 15) difference = end_date - start_date print(f"Days between {start_date.date()} and {end_date.date()}: {difference.days}")
OutputSuccess
Important Notes
When printing datetime objects, the exact time will depend on when the code runs.
You can specify days, seconds, microseconds, milliseconds, minutes, hours, and weeks in timedelta.
Subtracting two datetime objects gives a timedelta representing the difference.
Summary
Date arithmetic uses timedelta to add or subtract time from dates.
You can find differences between dates or shift dates forward or backward easily.
This is useful for scheduling, deadlines, and measuring time intervals.