ColdFusion:从cfloop获取变量字符串值

时间:2013-04-18 18:32:31

标签: coldfusion coldfusion-9 cfml

以下代码输出当月的周末日期。

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>

输出:
6 7 13 14 20 21 27 28

我正在尝试的是将cfloop的值传递给一个变量。以下代码仅显示上周末的日期值。

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>

输出
28

问题,我在上一个cfloop代码中需要修复什么,以便将循环值传递给jsWeekendDates变量?

非常感谢任何帮助 谢谢。

1 个答案:

答案 0 :(得分:2)

我自己想通了。享受。

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