Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to add/subtract a day in Python

To add or subtract a day in Python, you can use the datetime module, specifically the datetime.timedelta class. This class represents a duration of time and can be used to perform arithmetic with datetime objects.

Here's an example of how to add and subtract a day from a date using the datetime module:

from datetime import datetime, timedelta

# Get the current date and time
now = datetime.now()

# Add a day
tomorrow = now + timedelta(days=1)
print("Tomorrow:", tomorrow)

# Subtract a day
yesterday = now - timedelta(days=1)
print("Yesterday:", yesterday)

In this example, we first import the necessary classes from the datetime module. We then get the current date and time using datetime.now(). To add a day, we create a timedelta object with a duration of 1 day and add it to the current datetime object. To subtract a day, we create a timedelta object with a duration of 1 day and subtract it from the current datetime object.

If you only need to work with dates (not datetimes), you can use the date class from the datetime module:

from datetime import date, timedelta

# Get the current date
today = date.today()

# Add a day
tomorrow = today + timedelta(days=1)
print("Tomorrow:", tomorrow)

# Subtract a day
yesterday = today - timedelta(days=1)
print("Yesterday:", yesterday)

In this example, we use the date.today() method to get the current date, and then add and subtract a day using timedelta objects, just like in the previous example.

  1. Python Add Day to Date:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    new_date = current_date + timedelta(days=1)
    
    print("Current Date:", current_date)
    print("New Date (after adding one day):", new_date)
    
  2. Subtract a Day from Date in Python:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    new_date = current_date - timedelta(days=1)
    
    print("Current Date:", current_date)
    print("New Date (after subtracting one day):", new_date)
    
  3. Python Datetime Add One Day:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    new_date = current_date + timedelta(days=1)
    
    print("Current Date:", current_date)
    print("New Date (after adding one day):", new_date)
    
  4. Add and Subtract Days from a Date in Python:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    add_days = current_date + timedelta(days=5)
    subtract_days = current_date - timedelta(days=3)
    
    print("Current Date:", current_date)
    print("Date after adding 5 days:", add_days)
    print("Date after subtracting 3 days:", subtract_days)
    
  5. Python Timedelta Example for Adding Days:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    delta = timedelta(days=7)
    new_date = current_date + delta
    
    print("Current Date:", current_date)
    print("New Date (after adding 7 days):", new_date)
    
  6. Calculate Yesterday's Date in Python:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    yesterday = current_date - timedelta(days=1)
    
    print("Current Date:", current_date)
    print("Yesterday's Date:", yesterday)
    
  7. Python dateutil.relativedelta Add and Subtract Days:

    from datetime import datetime
    from dateutil.relativedelta import relativedelta
    
    current_date = datetime.now()
    new_date = current_date + relativedelta(days=3)
    
    print("Current Date:", current_date)
    print("New Date (after adding 3 days):", new_date)
    
  8. Adding and Subtracting Days from a Specific Date in Python:

    from datetime import datetime, timedelta
    
    specific_date = datetime(2022, 1, 1)
    add_days = specific_date + timedelta(days=10)
    subtract_days = specific_date - timedelta(days=5)
    
    print("Specific Date:", specific_date)
    print("Date after adding 10 days:", add_days)
    print("Date after subtracting 5 days:", subtract_days)
    
  9. Python Add One Day to Current Date:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    new_date = current_date + timedelta(days=1)
    
    print("Current Date:", current_date)
    print("New Date (after adding one day):", new_date)
    
  10. Subtracting Days Using datetime.timedelta in Python:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    new_date = current_date - timedelta(days=2)
    
    print("Current Date:", current_date)
    print("New Date (after subtracting 2 days):", new_date)
    
  11. Add and Subtract Days from a String Date in Python:

    from datetime import datetime, timedelta
    
    date_str = "2022-01-15"
    specific_date = datetime.strptime(date_str, "%Y-%m-%d")
    
    add_days = specific_date + timedelta(days=7)
    subtract_days = specific_date - timedelta(days=3)
    
    print("Specific Date:", specific_date)
    print("Date after adding 7 days:", add_days)
    print("Date after subtracting 3 days:", subtract_days)
    
  12. Python Date Arithmetic for Adding and Subtracting Days:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    add_days = current_date + timedelta(days=5)
    subtract_days = current_date - timedelta(days=3)
    
    print("Current Date:", current_date)
    print("Date after adding 5 days:", add_days)
    print("Date after subtracting 3 days:", subtract_days)
    
  13. Calculate Tomorrow's Date in Python:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    tomorrow = current_date + timedelta(days=1)
    
    print("Current Date:", current_date)
    print("Tomorrow's Date:", tomorrow)
    
  14. Python Arrow Module Add and Subtract Days:

    import arrow
    
    current_date = arrow.now()
    new_date = current_date.shift(days=3)
    
    print("Current Date:", current_date)
    print("New Date (after adding 3 days):", new_date)
    
  15. Add and Subtract Days Using Pandas in Python:

    import pandas as pd
    
    current_date = pd.to_datetime("2022-01-01")
    add_days = current_date + pd.DateOffset(days=5)
    subtract_days = current_date - pd.DateOffset(days=2)
    
    print("Current Date:", current_date)
    print("Date after adding 5 days:", add_days)
    print("Date after subtracting 2 days:", subtract_days)
    
  16. Adding and Subtracting Days in a Loop in Python:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    
    for i in range(1, 6):
        new_date = current_date + timedelta(days=i)
        print(f"Date after adding {i} days:", new_date)
    
  17. Python Date Manipulation for Adding and Subtracting Days:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    add_days = current_date + timedelta(days=5)
    subtract_days = current_date - timedelta(days=3)
    
    print("Current Date:", current_date)
    print("Date after adding 5 days:", add_days)
    print("Date after subtracting 3 days:", subtract_days)
    
  18. Subtract a Week or Month from a Date in Python:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    subtract_week = current_date - timedelta(weeks=1)
    subtract_month = current_date - timedelta(days=30)
    
    print("Current Date:", current_date)
    print("Date after subtracting a week:", subtract_week)
    print("Date after subtracting a month:", subtract_month)
    
  19. Python Calendar Module for Adding and Subtracting Days:

    from datetime import datetime, timedelta
    import calendar
    
    current_date = datetime.now()
    days_to_add = 10
    
    # Add days using calendar module
    new_date = current_date + timedelta(days=days_to_add)
    print("Current Date:", current_date)
    print(f"New Date (after adding {days_to_add} days):", new_date)
    
  20. Handling Leap Years When Adding or Subtracting Days in Python:

    from datetime import datetime, timedelta
    
    current_date = datetime.now()
    days_to_add = 365  # Considering a year
    
    # Handling leap years
    new_date = current_date + timedelta(days=days_to_add)
    
    print("Current Date:", current_date)
    print(f"New Date (after adding {days_to_add} days):", new_date)