I am using ColdFusion 9.0.1.
I am creating a contest each Monday at midnight. I need to use ColdFusion (but I am sure the logic is the same for other languages) to find the date of the most recent past Monday. Once I determine that date, I will drop that date into a SQL Statement to get the current standings and past results.
So, what functions do I need to find the most recent past Monday?
ANSWER
Dates = structNew();
Dates.CurrentDay = dateFormat(now(), "yyyy-mm-dd");
// LOOP MAX OF SEVEN TIMES
for (i = 1; i lte 7; i++) {
// IF CURRENT DAY OF WEEK IS MONDAY SET AND BREAK
if (dayOfWeek(Dates.CurrentDay) == 2) {
Dates.BikeOfTheWeekDate = Dates.CurrentDay;
break;
// IF CURRENT DAY OF WEEK IS NOT MONDAY SUBTRACT DAY
} else {
Dates.CurrentDay = dateAdd("d", -1, Dates.CurrentDay);
}
}
Pseudocode:
In ColdFusion, substract one day with
DateAdd("d", -1, date)and check for Monday withDayOfWeek(date)which returns 2 for Monday.