4

I have dates as a list in the following format:

09-2012,10-2012,01-2013 

What will be the better way to access the minimum and maximum dates from the list?

4 Answers 4

6

I'm not really sure which is faster. Here is my list-only-solution below , or you could convert the list to an array and work with that.

This does the job though:

<CFSET dates = "" />
<CFLOOP list="09-2012,10-2012,01-2013" index="date">
    <CFSET dates = listappend(dates,DateFormat(createDate(listlast(date,"-"),listfirst(date,"-"),1), "yyyy-mm-dd")) />
</CFLOOP>
<CFSET dates = listsort(dates,"numeric") />
<CFSET min_date = listfirst(dates) />
<CFSET max_date = listlast(dates) />
5

I would recommend transforming the dates in the list to yyyy-mm-dd.

Then you can just sort the list with ListSort and you will get the first and last date by listfirst and listlast

1

Similar to Seybsen's answer, only going down the regex path:

<cfscript>
var data = "09-2012,10-2012,01-2013";
data = listSort( reReplace( data, '([0-9][0-9])-([0-9][0-9][0-9][0-9])', '\2-\1', 'all' ), 'numeric', 'asc' );
var minDate = listFirst( data );
var maxDate = listLast( data );
</cfscript>
0

I adapted @Seybsen's answer to find the max date from a query without doing a query of queries:

<cfset maxCompletedDate = ListLast(ListSort(ValueList(queryName.completedDate), "numeric")) />

Thought I would put this out there if anyone else is looking of the same thing but, again, credit @Seybsen.

BTW, the dates in the database where formatted yyyy-mm-dd H:mm:ss. Timestamps like 2013-09-15 18:43:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.