Skip to main content
Inspiring
January 27, 2011
Question

I need to know the date of the last Monday

  • January 27, 2011
  • 2 replies
  • 1052 views

I need to know the date of the last Monday

If today is Monday then I need todays date

If today is Tuesday then I need yesterdays date etc....

If today is Sunday then I need to know the date 6 days ago etc....

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    January 28, 2011

    <cfset todayDayNumber  = dayOfWeek(now())>

    <!--- since Monday's day-of-week is 2 --->
    <cfset lastMondayRelativeDayNumber = todayDayNumber - 2>

    <!--- adjust if today is Sunday --->
    <cfif todayDayNumber is 1>
        <cfset lastMondayRelativeDayNumber = 6>
    </cfif>

    <cfset lastMonday = dateAdd("d",-lastMondayRelativeDayNumber,now())>

    ilssac
    Inspiring
    January 27, 2011

    the day() function will tell you what day of now() is as an integer: 1=Sunday 7=Saturaday.

    You know that Monday is 2.  The difference between today's number and Monday's number (with a little mathmatical juggling to wrap Sunday to the previous week  -- I would probably just convert -1 to 6 with a simple if statement after substracting 2 from the day() value.).

    Subtracting (adding a negative value) of this difference with the dateAdd() function will give you the date of that previous Monday.