Copy link to clipboard
Copied
I want to send a scheduled email on the Friday before the first Sunday of each month.
I presume if I can find the first Sunday I can do a simple dateadd to get the Friday - I just need to learn how to find the first Sunday.
Thanks
Copy link to clipboard
Copied
Start by googling "coldfusion date functions". When you see a page that lists them all, read each one and think of which ones might be useful.
Personally, I'd start with createdate, dateadd(), now(), year(), month(), and dayofweek(). But that's just me.
If you are having trouble figuring out the logic, drawing a flowchart might help.
Copy link to clipboard
Copied
Working out the date of the first day of the month is easy 😉
Working out which day of the week that happens to be is a single function call.
Working out the offset from that DOW to the nearest following Sunday is a simple mathematical expression
Ditto working out where the preceding Friday lies.
Dan's listed some functions to investigate...
--
Adam
Copy link to clipboard
Copied
<cffunction name="firstSundayOfMonth">
<cfargument name="month" required="yes">
<cfargument name="year" required="yes">
<cfset var dateFirstDayOfMonth = createDate(#arguments.year#,#arguments.month#,1)>
<cfset var firstDayWeekNumber = dayOfWeek(dateFirstDayOfMonth)>
<!---offset = number of days from first day to first Sunday--->
<cfset var offset = (8 - firstDayWeekNumber) mod 7>
<cfset var dateFirstSundayOfMonth = DateAdd("d", offset, dateFirstDayOfMonth)>
<cfreturn dateFirstSundayOfMonth>
</cffunction>
Date of first Sunday in June 2011 is <cfoutput>#firstSundayOfMonth(6,2011)#</cfoutput> (without format).<br>
Date of first Sunday in August 2010 is <cfoutput>#dateFormat(firstSundayOfMonth(8,2010),"dd mmm yyyy")#</cfoutput> (formatted).
Copy link to clipboard
Copied
Thank you for all the helpful asnwers.
Copy link to clipboard
Copied
ctreeves wrote:
Thank you for all the helpful asnwers.
It's good to be helpful, it's even better to answer your question. Have we?