Drop down list with dates

Hello all!

My apologies if this is posted elsewhere in the forum, but I went through several articles without luck.

I am attempting to populate a drop-down component with a list of dates starting with current day and each date going back 7 days. When the end-user selects a date, the plan is for the date to be passed to the SQL query that is populating a table on that page, so only the records from the selected date are provided.

As this drop-down will be needed on several windows, I have created a script called DateSelection in the project that pulls the desired date range (code below):

import datetime

date = datetime.datetime.today()
date_range = 7
selection = [date - datetime.timedelta(days=x) for x in range(date_range)]
for date in selection:
print date.strftime("%m/%d/%Y")

I am struggling with getting this bound to the drop-down list button on the window, so when the end-user clicks on the arrow, they are presented with the dates and can select one. I have tried a few things without much success.

Has anyone else ran into something similar? What am I missing?

Hi Superman,

A dropdown requires a dataset, so you need to format your function into a dataset, something like this:

import datetime
	
def DateSelection():
	date = datetime.datetime.today()
	date_range = 7
	selection = [date - datetime.timedelta(days=x) for x in range(date_range)]
	
	rows = []
	for date in selection:
		row = [date.strftime("%m/%d/%Y")]
		rows.append(row)
	
	ds = system.dataset.toDataSet(['Date'], rows)
	
	return ds

Then on the Data binding on the dropdown run the function in an expression:

runScript('project.MyFunctions.DateSelection',0)
1 Like

One thing with David’s solution is that you will need to put the import inside the function.

7.9 added some date functionality you could use as well. Also, if this is something used elsewhere, but needing a different number of rows, I’d suggest making it an optional parameter.

def DateSelection(dateRange = 7):
	dateIn = system.date.midnight(system.date.now())

	selection = [[system.date.format(system.date.addDays(dateIn, -x), 'MM/dd/YYYY')] for x in range(dateRange)]
	
	return system.dataset.toDataSet(['Date'], selection)

Thanks DavidWisely!

I knew I was overlooking something, and putting the function into a dataset was the trick!

Thanks JordanCClark for the suggestion on putting the import inside of the function.

1 Like