How to add Days to a Date in Python?

How to add Days to a Date in Python?

In Python, you can add days to a date using the datetime module. Here's how you can do it:

  1. First, you create a date or datetime object.
  2. Then, you add a timedelta object (representing a duration) to it.

Here's an example:

from datetime import datetime, timedelta # Get the current date and time current_datetime = datetime.now() print(f"Current datetime: {current_datetime}") # Add 5 days to the current date new_datetime = current_datetime + timedelta(days=5) print(f"New datetime after adding 5 days: {new_datetime}") # If you only want to work with the date part current_date = current_datetime.date() print(f"Current date: {current_date}") new_date = current_date + timedelta(days=5) print(f"New date after adding 5 days: {new_date}") 

This will add 5 days to the current date and print both the original date and the new date. Adjust the days argument in the timedelta function to add a different number of days.


More Tags

gensim hierarchical-clustering apache-pig image-processing nuget-package-restore scala-collections spring-mvc redraw computation-theory wget

More Programming Guides

Other Guides

More Programming Examples