Skip to main content
Inspiring
February 9, 2011
Question

First Sunday

  • February 9, 2011
  • 4 replies
  • 1001 views

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

    This topic has been closed for replies.

    4 replies

    ctreevesAuthor
    Inspiring
    February 24, 2011

    Thank you for all the helpful asnwers.

    BKBK
    Community Expert
    Community Expert
    February 25, 2011

    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?

    BKBK
    Community Expert
    Community Expert
    February 13, 2011

    <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).

    Inspiring
    February 9, 2011

    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

    Inspiring
    February 9, 2011

    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.