0

我想更改DayOfWeek()功能,以便我可以根据自己想要的一周的第一天获取日期。所以我在这段代码中所做的是我正在设置 a startDateendDate并且selectDays我想检查它。这是我的代码:

<cfscript>

function Mydayofweek(date, day_1)
{   
    return (((DayOfWeek(date) + (7 -day_1)) MOD 7) +1);
}

</cfscript>

<cfset startDate = '07/01/2013'>
<cfset endDate = '07/25/2013'>
<Cfset mydates = ''>
<cfset selectDays = '2,6'>

<cfset MyWeekFirstDay = 6> <!---I selected Friday = 6 --->
<cfset new = ''>

<cfoutput>
    <cfloop list="#selectDays#" delimiters="," index="d">
        <cfset new &=  '#Mydayofweek(d, MyWeekFirstDay)#,' >
    </cfloop>


<cfif new NEQ ''>
    <cfset ScheduleDate = left(new, (len(new)-1) )>
</cfif>

<cfdump var="#ScheduleDate#"><br />
</cfoutput>
<cfset AppendToMyDates = false>
 <cfloop from="#startDate#" to="#endDate#" index="day">
 <cfif AppendToMyDates is false and DayOfWeek(day) is ListFirst(selectDays)>
 <cfset AppendToMyDates = true>
 </cfif>

    <cfif listfind(ScheduleDate, DayOfWeek(day), ',') NEQ 0 and AppendToMyDates is true>

      <cfset mydates &= "#dateformat(day, 'mmm, dd, yyyy dddd')#,<br />">

    </cfif>
    </cfloop><cfoutput>#mydates#</cfoutput> 

这是用 ColdFusion 编写的。该代码生成以下输出:

4,1
Jul, 03, 2013 Wednesday,
Jul, 07, 2013 Sunday,
Jul, 10, 2013 Wednesday,
Jul, 14, 2013 Sunday,
Jul, 17, 2013 Wednesday,
Jul, 21, 2013 Sunday,
Jul, 24, 2013 Wednesday,

输出应该是这样的,因为我选择 Friday = 1 到 Thursday = 7 所以上面的日子selectDays = '2,6'现在应该指向 selectDays = '7,4' 相对于我的第一天2,6

7,4
Jul, 06, 2013 Saturday,
Jul, 10, 2013 Wednesday,
Jul, 13, 2013 Saturday,
Jul, 17, 2013 Wednesday,
Jul, 20, 2013 Saturday,
Jul, 24, 2013 Wednesday,

我已经设置了我的selectDays = '2,6'意思是我想要获取日期,Saturday因为Wednesday我已经将 6 设置为我一周的第一天,它从星期五开始(默认情况下是星期天)。我的日子从星期日开始Sunday = 1 , Monday = 2 , Tuesday = 3 , Wednesday = 4 , Thursday = 5 , Friday = 6 , Saturday = 7,现在我从Friday = 1 , Saturday = 2 ,Sunday = 3 , Monday = 4 , Tuesday = 5 , Wednesday = 6 , Thursday = 7脚本函数中更改了日期。实际上,我认为我的脚本函数中存在我不理解的错误。请帮我找出问题并解决,谢谢

更新

4

3 回答 3

1

您只需要将脚本调用到循环中。无需制作任何功能,只需像这样更改代码即可。复制此代码希望这将解决您的问题。

<cfscript>
  function Mydayofweek(date, day_1)
  {   
     return (((DayOfWeek(date) + (7 -day_1)) MOD 7) +1);
  } 
</cfscript>

<cfset startDate = '07/01/2013'>
<cfset endDate = '07/25/2013'>
<Cfset mydates = ''>
<cfset selectDays = '2,6'>
<cfset MyWeekFirstDay = 6><!---I selected Friday = 6 --->


<cfloop from="#startDate#" to="#endDate#" index="day">
  <cfif listfind(selectDays, Mydayofweek(day,MyWeekFirstDay), ',') NEQ 0 >
    <cfset mydates &= "#dateformat(day, 'mmm, dd, yyyy dddd')#,<br />">
  </cfif>
</cfloop>
<cfoutput>#mydates#</cfoutput> 
于 2013-06-27T08:50:26.397 回答
1

有了 selectDays 可能并不总是相同的新信息,我会做这样的事情:

<cfset AppendToMyDates = false>
 <cfloop from="#startDate#" to="#endDate#" index="day">
 <cfif AppendToMyDates is false and DayOfWeek(day) is ListFirst(SelectDays)>
 <cfset AppendToMyDates = true>
 </cfif>

    <cfif listfind(selectDays, DayOfWeek(day), ',') NEQ 0 and AppendToMyDates is true>
      <cfset mydates &= "#dateformat(day, 'mmm, dd, yyyy dddd')#,<br />">
    </cfif>
  </cfloop>

编辑从这里开始

如果您希望一周的开始是一个变量,您希望使用不同的名称编写您自己的 DayOfWeek() 版本。结构将是这样的:

<cffunction name="DayOfWeekModified returntype="numeric">
<cfargument name="WeekStartsOn" type="numeric" required="yes">
<cfscript>
var DayNumber = 0;
code to generate it based on arguments.WeekStartsOn
return DayNumber;
<cfscript>
<cffunction>

然后在循环中调用此函数而不是 DayOfWeek()。

于 2013-06-26T13:11:09.133 回答
0

你遇到的问题是你的cfif情况。您正在循环查看日期,然后检查给定日期是星期三还是星期五,并且您得到的结果是正确的。由于您想从星期五开始(忽略第一个星期三),您需要将其添加到您的代码中。这可能对您有用:

<cfset startDate = '06/11/2013'>
<cfset endDate = '06/25/2013'>
<cfset mydates = ''>
<cfset selectDays = '6,4'>

<cfloop from="#startDate#" to="#endDate#" index="day">
    <cfif listfind(selectDays, DayOfWeek(day), ',') NEQ 0>
        <cfif mydates NEQ "" OR DayOfWeek(day) EQ "6">
            <cfset mydates &= "#dateformat(day, 'mmm, dd, yyyy dddd')#,<br />">
        </cfif>
    </cfif>
</cfloop>
<cfoutput>#mydates#</cfoutput> 

cfif我在设置周围添加了一个附加条件mydates。这段代码mydates NEQ ""正在检查是否mydates不为空,这意味着我们已经满足了下一个条件。如果mydates为空,则还要检查给定日期是否为星期五DayOfWeek(day) EQ "6"。这应该保证输入的第一个日期mydates是星期五。

不知道我对这段代码的感觉如何,但它似乎对你的示例有用。

于 2013-06-26T12:46:14.337 回答