Question

I need to determine if a set of dates contain two different weeks.

For example:

When returning a set of records from a database that contain a date, there needs to be something to distinguish between the different weeks.

<cfloop query="datesExample">

   <cfif DateDiff("d",DateFormat(lastDate),DateFormat(OriginalDate)) GTE 7>
       <hr />
   </cfif>
   <p>#OrginalDate#</p>

   <cfset lastDate = DateFormat(OrginalDate) />
</cfloop>

To me, this seems like all the logic I'd need to add to determine if there is a new week.

Although, I'm not getting any results from this.

Anyone have an ideas?

Update:

This is my actual if statement:

<cfif DayofWeek(lastShiftDate) NEQ DayOfWeek(Time_In) AND DateDiff("d",lastShiftDate, Time_In) GTE 7>
Was it helpful?

Solution 6

It was much more simple than what I thought. All I had to do was get the value of the first date. So, <cfif query.RecordCount EQ 1><cfset firstDate = Now() /></cfif> and then do a dateDiff() with the lastShiftDate

Thanks for everyone's help.

OTHER TIPS

I would QueryAddColumn to add an additional column onto the end of your query, then loop through and set the start of the week for each record in the query. Something like this:

<cfset datesExample = QueryNew("lastshiftdate", "date") />
<cfset QueryAddRow(datesExample) />
<cfset QuerySetCell(datesExample, "lastshiftdate", "2009-01-15") />
<cfset QueryAddRow(datesExample) />
<cfset QuerySetCell(datesExample, "lastshiftdate", "2009-01-20") />
<cfset QueryAddColumn(datesExample, "StartofWeek", "time", ArrayNew(1)) />
<cfloop query="datesExample">
    <cfset QuerySetCell(datesExample, "StartofWeek", DateAdd("d", -(DayOfWeek(lastshiftdate) - 1), lastshiftdate), CurrentRow) />
</cfloop>

<cfdump var="#datesExample#">

Then when you use the cfoutput, you can just group by the StartofWeek column without doing all of the goofy conditional logic.

Dan

when the dayofweek of dateA is different than dayofweek of dateB , and their DateDiff is larger than 7?

Then dateA and dateB are in different week?

The code you presented has several syntax errors. If you copied it from your source, you might try turning off whatever error trapping is keeping you from seeing the errors.

First, to address syntax: Your cfif is not closed. DateFormat() takes two arguments -- the date object and the date mask. If this is not in a cfoutput block, you'll just display the literal #OrginalDate#.

To address the logic:

I'm assuming OriginalDate is a field in the query return.

I'm not sure exactly what you are trying to do. Your code would print a horizontal line instead of the date anytime there was more than 6 days between two records. However, you will never print a horizontal line as long as the dates are closer together. So, if you had ever Mon/Wed/Fri in the database, you would never get an HR, because the datediff would be two or three days, never seven.

There are several approaches to deal with this. If you clarify your purpose, we might be able to help you better. For example, what defines a new week? Sat/Sun at midnight? Since the first date in the query? What are you trying to display?

Check out the Day of Week function

Check out the Week function: From a date/time object, determines the week number within the year.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top