Question

I want to change the DayOfWeek() function so that I can get dates according to my own desired first day of the week. So what I am doing in this code is that I am setting a startDate, endDate and selectDays that I want to check. Here is my code:

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

This is written in ColdFusion. That code generates this output:

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,

The Output Should be like this because i select Friday = 1 to Thursday = 7 so the above days selectDays = '2,6' should now point to selectDays = '7,4' with respect to my first days 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,

I have set my selectDays = '2,6' it means I want to get dates of Saturday and Wednesday because I have set 6 as my week first day and it start from Friday (by default it was sunday). My days start from Sunday Sunday = 1 , Monday = 2 , Tuesday = 3 , Wednesday = 4 , Thursday = 5 , Friday = 6 , Saturday = 7 and now I changed my date start from Friday = 1 , Saturday = 2 ,Sunday = 3 , Monday = 4 , Tuesday = 5 , Wednesday = 6 , Thursday = 7 in script function. Actually I think there is error in my script function that i don't understand. Please help me out to find the problem and it's solution thanks

UPDATED

Was it helpful?

Solution

You just need to call your script into your loop. No need to make any function just change the code like this one. Copy this code hope this will solve your problem.

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

OTHER TIPS

With the new information that selectDays might not always be the same, I'd do something like this:

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

Edit starts here

If you want the start of the week to be a variable, you want to write your own version of DayOfWeek() with a different name. The structure would be something like this:

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

You then call this function instead of DayOfWeek() in your loop.

The issue you are having is with your cfif condition. You are looping over the dates and then checking if the given date is a Wednesday or Friday and the results you are getting are correct. Since you want to start with a Friday (ignoring the first Wednesday) you need to add that to your code. This may work for you:

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

I added an additional cfif condition around the setting of mydates. This code mydates NEQ "" is checking to see if mydates is not empty, meaning we have already satisified the next condition. If mydates is empty then also check to see if the given date is a Friday with DayOfWeek(day) EQ "6". This should guarantee that the first date entered in mydates is a Friday.

Not sure how I feel about this code but it seemed to work for me with your example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top