0 votes
1 view
in Python by (45.1k points)

I need help with a program.

How do I add 3 weeks (21 days) to any given date when the user can control the date?

The user will enter the date YYYY-MM-DD.

Below I'm trying to locate the hyphen and make sure there is only 2. This is what I have so far but all it does is repeat itself, can someone tell me where I went wrong ?:

date = raw_input("Enter date: ")

i = 0

while i <= len(date):

    if date[i] != "-":

    i = i + 1

print date

Now I'm picking out the year, month, day. Is there an easier way to do this cause I need to account for the change months etc?

year = date[0:4]

month = date[5:7]

day = date[9:11]

thanks

1 Answer

0 votes
by (16.8k points)

Use datetime module to the task. You create a datetime aware object and add 21 days timedelta object to it.

>>> import datetime

>>> u = datetime.datetime.strptime("2011-01-01","%Y-%m-%d")

>>> d = datetime.timedelta(days=21)

>>> t = u + d

>>> print(t)

2011-01-22 00:00:00

Related questions

0 votes
1 answer
asked Jul 24, 2019 in Python by Eresh Kumar (45.1k points)
0 votes
1 answer
asked Jul 23, 2019 in Python by Eresh Kumar (45.1k points)
0 votes
1 answer
asked Jul 23, 2019 in Python by Eresh Kumar (45.1k points)
0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...