Here is a sample I created. It loops over an original array (you could make
it work with a list of course) and creates a new array that has 'combined'
data. Ie, singlular dates + ranges.
It then does a final loop just to make the 'pretty' formatted text.
<cfscript>
dates = [
createDate(2013, 6, 10),
createDate(2013, 6, 11),
createDate(2013, 6, 14),
createDate(2013, 6, 16),
createDate(2013, 6, 17),
createDate(2013, 6, 18),
createDate(2013, 6, 22),
createDate(2013, 6, 25),
createDate(2013, 6, 29)
];
writeDump(var=dates, label="Initial data");
//new array for our formatted data
newDates = [];
for(x=1; x<=arrayLen(dates); x++) {
if(x == 1) {
arrayAppend(newDates, {first:dates[1],last:dates[1]});
continue;
}
//is our date 1d past last date of previous?
if(dateDiff("d", newDates[arrayLen(newDates)].last, dates[x]) == 1) {
writeoutput('date diff is one');
newDates[arrayLen(newDates)].last = dates[x];
} else {
arrayAppend(newDates, {first:dates[x], last:dates[x]});
}
}
//At this point we are done...
writeDump(var=newdates, label="New data");
//But let's make it prettier
for(x=1; x<=arrayLen(newDates); x++) {
if(newDates[x].first == newDates[x].last) {
newDates[x].formatted = dateFormat(newDates[x].first, "long");
} else {
newDates[x].formatted = dateFormat(newDates[x].first, "long") & " - " &
dateFormat(newDates[x].last, "long");
}
}
//At this point we are done...
writeDump(var=newdates, label="Pretty New data");
</cfscript>
On Sat, May 18, 2013 at 9:40 AM, Lewis Billingsley <
[email protected]> wrote:
>
> Hello,
>
> Ive been tasked to display, in a table dates from a database, but the
> difficulty is that whenever there are 2 or more in a row, a range should be
> displayed.
>
> The table now looks like this:
>
> May 27, 2013
> June 1, 2013
> June 2, 2013
> June 3, 2013
> July 4, 2013
>
> It should look like this:
> May 27, 2013
> June 1, 2013 - June 3, 2013
> July 4, 2013
>
> I tried to get it into a list, so I could display listFirst, List,Last,
> but I cant find a way to compare the last record to the current record to
> get into a loop (if indeed, my loop is right)
> Heres the code:
> <tbody>
> <cfoutput query="currentClosures">
> <cfset thisDate = #holidayDate#>
> <cfset lastDate = #thisDate#>
> <cfset rangeList = ValueList(currentClosures.holidayDate)>
> <cfif DateDiff( "d", thisDate, lastDate) EQ 1>
> <cfloop query="currentClosures">
> <cfset rangeList = ListAppend(rangeList,
> #holidayDate#)>
> <cfset showDate = ListFirst(rangeList)>
> <cfset lastDate = DateAdd("d", -1, thisDate)>
> </cfloop>
> <cfelse>
> <cfset showDate = #holidayDate#>
> </cfif>
> <tr>
> <td class="left" scope="row">
>
> #showDate#
> </td>
>
> Thanks much,
> Lewis
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive:
http://www.houseoffusion.com/groups/cf-newbie/message.cfm/messageid:6058
Subscription: http://www.houseoffusion.com/groups/cf-newbie/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-newbie/unsubscribe.cfm