-5

I am trying to code a function called days15(). The function will be passed an argument called ‘myDateStr’. myDateStr is string representation of a date in the form 20170817 (that is YearMonthDay). The code in the function will create a datetime object from the string, it will then create a timedelta object with a length of 1 day. Then, it will use a list comprehension to produce a list of 15 datetime objects, starting with the date that is passed to the function

the function should return the following list.

[datetime.datetime(2017, 8, 17, 0, 0), datetime.datetime(2017, 8, 18, 0, 0), datetime.datetime(2017, 8, 19, 0, 0), datetime.datetime(2017, 8, 20, 0, 0), datetime.datetime(2017, 8, 21, 0, 0), datetime.datetime(2017, 8, 22, 0, 0), datetime.datetime(2017, 8, 23, 0, 0), datetime.datetime(2017, 8, 24, 0, 0), datetime.datetime(2017, 8, 25, 0, 0), datetime.datetime(2017, 8, 26, 0, 0), datetime.datetime(2017, 8, 27, 0, 0), datetime.datetime(2017, 8, 28, 0, 0), datetime.datetime(2017, 8, 29, 0, 0), datetime.datetime(2017, 8, 30, 0, 0), datetime.datetime(2017, 8, 31, 0, 0)]

I am stuck for the code. I have strted with the below.Please help. Thanks

from datetime import datetime, timedelta
myDateStr = '20170817'
def days15(myDateStr):
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Harry
  • 1
  • 1
    Possible duplicate of [how to convert a string date into datetime format in python?](https://stackoverflow.com/questions/19068269/how-to-convert-a-string-date-into-datetime-format-in-python) – roganjosh Aug 22 '18 at 12:37
  • I'm not sure what you're asking. You've imported the two methods that you need to accomplish this but appear not to have actually tried using them. – roganjosh Aug 22 '18 at 12:38
  • 1
    http://idownvotedbecau.se/noattempt/. Note that while you have started some code, you have not shown an attempt on anything inside the actual `days15()` function. – Roope Aug 22 '18 at 12:38
  • Why `datetime` and not `date`? – Klaus D. Aug 22 '18 at 12:41
  • Thanks guys. Actually I am new to python and have just started learning the date function. The question thrown is very tough for me to solve so I am in need of help to code it. Thanks – Harry Aug 22 '18 at 12:58

2 Answers2

0

As you said, there will be two steps to implement: firstly, convert the string date to a datetime object and secondly, iterate over the next 15 days using timedelta, with a list comprehension or a simple loop.

from datetime import datetime, timedelta
myDateStr = '20170817'

# Parse the string and return a datetime object 
def getDateTime(date):
    return datetime(int(date[:4]),int(date[4:6]),int(date[6:]))

# Iterate over the timedelta added to the starting date
def days15(myDateStr):
    return [getDateTime(myDateStr) + timedelta(days=x) for x in range(15)]
Tony Pellerin
  • 231
  • 1
  • 7
  • While this code may answer the question, providing additional context regarding *how* and *why* it solves the problem would improve the answer's long-term value. – Alexander Aug 22 '18 at 16:38
0

Pandas will help you in converting strings to datetime, so first you need to import it:

from datetime import datetime, timedelta
import pandas as pd

myDateStr = '20170817'

Then you can initialize an empty list that you'll later append:

datelist = []

And then you write a function:

def days15(myDateStr):
    #converting to datetime 
    date = pd.to_datetime(myDateStr)
    #loop to create 15 datetimes
    for i in range(15):
    newdate = date + timedelta(days=i)
    #adding new dates to the list
    datelist.append(newdate)

and then you can call your function and get a list of 15 datetimes:

 days15(myDateStr)
Piotrek
  • 1,400
  • 9
  • 16