Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

I need to know the date of the last Monday

Participant ,
Jan 27, 2011 Jan 27, 2011

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

1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jan 27, 2011 Jan 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 27, 2011 Jan 27, 2011
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources