views:

276

answers:

6

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>
A: 

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?

Henry
I see one noticeable difference, but it only occurred in one spot of the loop. Needs to be in all occurrences.
Michael Stone
u might need to keep track of more than just the 'lastDate'. When you detect a different week, print <HR> and then store that new date to compare with all other dates? Assuming your date is sorted...
Henry
You shouldn't need to keep track of each date. I'll update my code with my actual conditional statement.
Michael Stone
How about pre-determine when's nextSunday, and then compare each date. On the first date that is later than the nextSunday, output <HR> and update nextSunday=nextSunday+7 days?
Henry
A: 

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?

Ben Doom
The syntax error was just a small mistake. I was coding it freehand in the textarea. I fixed it. In this case, I want an <hr /> for everything there is a new week found. So if your first record is 9/15/09 and your second record was 9/24/09, then it needs to include the <hr /> to distinguish that there are two weeks.
Michael Stone
A: 

Check out the Day of Week function

Jay
+1  A: 

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

Daniel Short
Looks good, but the app uses a loop inside of a cfoutput. The app is old and because of much it is used, can't be re-developed to a large extent. Nice suggestion though.
Michael Stone
It may still be possible by just changing the loop to a cfoutput to get the grouping you want. Regardless of all of that, you could still use the logic above to figure out the start of each week, and then just do your whole lastDate/originalDate business that you posted in the initial question. Either way, the "hard part" is getting the start of the week. After that it's just a matter of an if statement here or there.
Daniel Short
Attempting it, but I'm getting a missing argument name for the QuerySetCell "When using named parameters to a function, every parameter must have a name."
Michael Stone
I missed a comma, after "StartofWeek" and before DateAdd... I'll edit.
Daniel Short
I still get the exact same error. I've tried debugging it. I've tried using all the references I can find, and it should be working right, but it's still display the error.
Michael Stone
Hi Michael, I'm sorry for the bad code. I've edited the answer with the *correct* code. I've run this on my local machine without an error. That's what I get for re-typing code instead of copy/pasting.
Daniel Short
A: 

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

Antony
Was one of the first things I tried. Didn't work the way I needed it to.
Michael Stone
What result did you get?
Antony
The separated weeks would be be a day off. I believe it's because the days start on a Sunday, in this application they start on a Monday.
Michael Stone
do you get the same result with http://www.cfquickdocs.com/cf8/#DatePart ?
Antony
A: 

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.

Michael Stone