Question

The code below outputs weekend dates in the current month.

CODE:

<cfparam name="month" default="#DatePart('m', Now())#">
<cfparam name="year" default="#DatePart('yyyy', Now())#">

<cfset ThisMonthYear=CreateDate(year, month, '1')>
<cfset Days=DaysInMonth(ThisMonthYear)>

<cfset ThisDay = 1>
<cfloop condition="ThisDay LTE Days">
    <cfset presentDay = CreateDate(year, month, thisday)>
    <cfif DayOfWeek(presentDay) EQ '7'>
        <cfoutput>#ThisDay#</cfoutput>
    <cfelseif DayOfWeek(presentDay) EQ '1'>
        <cfoutput>#ThisDay#</cfoutput>
    </cfif>
    <cfset ThisDay = ThisDay + 1>
</cfloop>

OUTPUT:
6 7 13 14 20 21 27 28

What I'm trying is to pass value of this cfloop in one variable. The code below only displays the last weekend date value.

CODE:

<cfset ThisDay = 1>
<cfset weekDayOfMonth = "">
<cfloop condition="ThisDay LTE Days">
    <cfset presentDay = CreateDate(year, month, thisday)>
    <cfif DayOfWeek(presentDay) EQ '7'>
        <cfset weekDayOfMonth = ThisDay>
    <cfelseif DayOfWeek(presentDay) EQ '1'>
        <cfset weekDayOfMonth = ThisDay>
    </cfif>
    <cfset ThisDay = ThisDay + 1>
</cfloop>
<cfoutput>#weekDayOfMonth#</cfoutput>

OUTPUT
28

Question, what do I need fix in my last cfloop code so I can pass loop values into the jsWeekendDates variable?

Any help will be greatly appreciated.
Thank you.

Was it helpful?

Solution

Just figured on my own. Enjoy.

<cfset ThisDay = 1>
<cfset weekDay = "">
<cfloop condition='ThisDay LTE Days'>
    <cfset presentDay = CreateDate(year, month, thisday)>
    <cfif DayOfWeek(presentDay) EQ '1' OR DayOfWeek(presentDay) EQ '7'>
        <cfset weekDay = weekDay & " " & ThisDay">
    </cfif>
    <cfset ThisDay = ThisDay + 1>
</cfloop>
<cfoutput>#weekDay#</cfoutput>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top