troubleshooting Question

error with datetime python

Avatar of romeiovasu
romeiovasu asked on
Python
1 Comment1 Solution425 ViewsLast Modified:
here is my code can some one help me with the error please.
from download import constructYFURL
ticker = "AAPL"
start_date = "2016-01-01"
end_date = " 2017-03-06"
freq = "d"

yfURL = constructYFURL(ticker,start_date,end_date,freq)

print yfURL

i am getting an error saying
ValueError: time data ' 2017-03-06' does not match format '%Y-%m-%d'

here is the code for import constructYFURL
from datetime import datetime


def constructYFURL(ticker,start_date,end_date,freq):
    start_date = datetime.strptime(start_date,"%Y-%m-%d").date()
    end_date = datetime.strptime(end_date,"%Y-%m-%d").date()

    s=ticker.replace("^","%5E")

    if start_date.month-1<10:
        a="0"+str(start_date.month-1)
    else:
        a=str(start_date.month-1)
    # a represents the month portion - however the month count starts from 0
    # Also the month always has 2 digits
    b=str(start_date.day)

    c=str(start_date.year)
    # b and c represent the day and year parts of the start date
    if end_date.month - 1 < 10:
        d = "0" + str(end_date.month - 1)
    else:
        d = str(end_date.month - 1)
    # similarly we have to set up the month part for the end date
    e=str(end_date.day)

    f=str(end_date.year)
    # e and f represent the day and year parts of the end date
    g=freq
    # g represents the frequency d = daily, w= weekly, m=monthly

    # Finally let's set up the URL

    yfURL = "http://real-chart.finance.yahoo.com/table.csv?s="+s+"&a="+a+"&b="+b+"&c="+c+"&d="+d+"&e="+e+"&f="+f+"&g="+g+"&ignore=.csv"
    return yfURL
ASKER CERTIFIED SOLUTION
gelonida

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros