35

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 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

4 Answers 4

79

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
4
  • 1
    thank you so much, but i think i might have to do it the old fashion way... we were never taught datetime.
    – monica
    Mar 4, 2011 at 5:01
  • senthil - thanks for the encouragement ! i think i gonna go read head, i really dont want to write tons of code for one that can be so simplified. i cant thnk you enough for showing me.
    – monica
    Mar 4, 2011 at 5:13
  • @monica, if you didn't use datetime and timedelta, you would definitely need a bunch of code to account for the different lengths of the months - especially Feb during leap years, and incrementing the year if the 3 weeks would push you past the end of Dec. etc. Mar 4, 2011 at 5:23
  • @gnibbler, it looks like im doing it the long way, found out that im not allowed to use datetime
    – monica
    Mar 5, 2011 at 2:38
16

You can use a datetime.timedelta object to represent 3 weeks and then just add that to the datetime object that represents the user's input.

import datetime

date = raw_input("Enter date: ")
aDate = datetime.datetime.strptime(date,"%Y-%m-%d")
threeWeeks = datetime.timedelta(weeks = 3)

print aDate + threeWeeks

See http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior for details about using the strptime method.

1
  • q, thanks for all you help, and responding so quickly. im very grateful.
    – monica
    Mar 4, 2011 at 5:06
5

Try this, I am sure its the shortest and easiest way to go


from dateutil.relativedelta import relativedelta

period = date.today() + relativedelta(weeks=+1)

1
  • I really appreciate this, Randall. Sometimes a third party package makes everything so much more readable. Apr 26, 2021 at 13:41
-1

you can use datetime.strptime to get input from user as date

from datetime import datetime
i = str(raw_input('date'))
try:
    dt_start = datetime.strptime(i, '%Y, %m, %d')
except ValueError:
    print "Incorrect format"

and then to add 3 weeks (21 days)

dt_start = dt_start + datetime.timedelta(days=21)

There you go

1
  • (1) you don't need str() around raw_input(). (2) both strptime() and timedelta() are already shown here -- I don't see what your answer adds here to the answer posted 4 years ago.
    – jfs
    Sep 18, 2015 at 15:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.