Hello all and thanks in advance for any help you can provide. I'm new to programming in Python and am looking to put todays date parameter passed into a URL string...how would I go about accomplishing this. This is some code I've sort of been messing around with:

[start code]
import datetime

def get_prices():
day = datetime.date.today()
tday = strftime("Y%m%d)
url = 'http://samplewebsite/example/example&startdate=20100301&enddate=20100301&market_id=X'

[end code]

So as you can see im very new to this but learning more and more everyday but I'd like to get that URL variable to pass in the tday variable that already should have the date formatted to a string that should fit into it. Any help in getting that date into the URL where it has the 20100301 dates that would be great.

Thanks.

You need to import the time module since the strftime function is in that module. Then make sure you call the strftime function with the module name before it (ex: module.function() -> time.strftime()). To put today's date in the url, make the dates in the url variables and set them to the date you got with strftime.

Here's what I came up with:

import datetime, time

def get_prices():
    day = datetime.date.today() 
    today = time.strftime("%Y%m%d")
    today_url = "http://samplewebsite.com/example/example&startdate=" + today + "&enddate=" + today + "&market_id=X"
    print today_url #test

get_prices()

Output:

>>>
http://samplewebsite.com/example/example&startdate=20100329&enddate=20100329&market_id=X

There really isn't a need for module datetime in your example ...

import time

# create yyyymmdd string
# uses today's time tuple by default
today = time.strftime("%Y%m%d")
print today  # eg. 20100329

Awesome thanks for the quick responses everyone and all the examples worked fine. Thanks again.

This article has been dead for over six months. Start a new discussion instead.