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

Tim Swast
  • 14,091
  • 4
  • 38
  • 61
monica
  • 371
  • 1
  • 3
  • 4

4 Answers4

78

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
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • 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 04 '11 at 05: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 04 '11 at 05: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. – John La Rooy Mar 04 '11 at 05:23
  • @gnibbler, it looks like im doing it the long way, found out that im not allowed to use datetime – monica Mar 05 '11 at 02: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.

Alex Q
  • 3,080
  • 2
  • 27
  • 29
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)

Randall Moore
  • 275
  • 4
  • 6
-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

noobsee
  • 806
  • 15
  • 29
  • (1) you don't need `str()` around `raw_input()`. (2) both `strptime()` and `timedelta()` are already shown [here](http://stackoverflow.com/a/5189806/4279) -- I don't see what your answer adds here to the answer posted 4 years ago. – jfs Sep 18 '15 at 15:53