Adding Days to a Date in Python

Adding days to a date in Python

The previous answers are correct but it's generally a better practice to do:

import datetime

Then you'll have, using datetime.timedelta:

date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")

end_date = date_1 + datetime.timedelta(days=10)

Add 1 day to my date in Python

You need the datetime module from the standard library. Load the date string via strptime(), use timedelta to add a day, then use strftime() to dump the date back to a string:

>>> from datetime import datetime, timedelta
>>> s = '2004/03/30'
>>> date = datetime.strptime(s, "%Y/%m/%d")
>>> modified_date = date + timedelta(days=1)
>>> datetime.strftime(modified_date, "%Y/%m/%d")
'2004/03/31'

python: adding any number of days to date

x=20100103
x2 = int((datetime.datetime.strptime(str(x),"%Y%m%d") + datetime.timedelta(days=10)).strftime("%Y%m%d"))

to break it down

x=20100103
x_as_datetime = datetime.datetime.strptime(str(x),"%Y%m%d")
new_datetime = x_as_datetime + datetime.timedelta(days=10) #add your timedelta
x2 = new_datetime.strftime("%Y%m%d") # format it back how you want
int(x2) # if you just want an integer ...

Adding days to datetime

line.planned_date is a Unicode string object. You need to convert it to a datetime object and then add using timedelta.

Ex:

import datetime
planned = (datetime.datetime.strptime(line.planned_date, '%Y-%m-%d') + datetime.timedelta(days=daysz) ).strftime('%Y-%m-%d')

Adding hours and days to the python datetime

from datetime import datetime
from datetime import timedelta

origin_date = datetime.strptime("2021-05-06 17:30","%Y-%m-%d %H:%M")
three_hour_later = origin_date + timedelta(hours=3)

print(datetime.strftime(three_hour_later,"%Y-%m-%d %H:%M"))

Please check this link.
https://docs.python.org/3/library/datetime.html

Adding Days to a date without using a library

This appears to have fixed your issue:

Days_in_Month[1] should be 28 or 29, not 27 or 28 and it needed to be corrected both ways for each year.

I wrote my own isBisextile() which is obviously not fully compliant with all of the rules of leap-years that are % 100 or % 400 but I assume you have that covered in your version that you haven't shown us.

year = 1980
month = 7
date = 2

Days_in_Month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def isBisextile(year):
return True if year%4 == 0 else False

if isBisextile(year):
Days_in_Month[1] = 29

days = int(input("Enter number of days:\n"))

while days > 0:

if isBisextile(year):
Days_in_Month[1] = 29
else:
Days_in_Month[1] = 28

date += 1

if date > int(Days_in_Month[month-1]):
month += 1
if month > 12:
year += 1
month = 1
date = 1
days -= 1

print("{}/{}/{}".format(date, month, year))

For 2/7/1980 and number of days: 1460 it gives 1/7/1984 as expected.

How to add a 1 day to today's date in python?

You are trying to do date operations on strings and not using the result of your formatting call:

s = date.today()
modified_date = s + timedelta(days=1)
modified_date = modified_date.strftime("%Y-%m-%d") # this would be more common
# modified_date = datetime.strftime(modified_date, "%Y-%m-%d")

print(modified_date)


Related Topics



Leave a reply



Submit