Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

coldfusion DayOfWeek

I have a little dilemma here. I have a 18 date slots to fulfill and the user select a day of the week on which they would want to appear also the initial start date is provided.

For example the start date is 5/1/2015 and they select Monday and Wednesday. Then the date should work out as follow:
5/4/2015 and 5/6/2015 and so on until 18 dates are fullfilled
0
erikTsomik
Asked:
erikTsomik
  • 11
  • 9
1 Solution
 
erikTsomikSystem Architect, CF programmer Author Commented:
I came up with this example but does not seems to work

<cfoutput>
	<cfset locInitDate = '5/1/2015'>
	<cfset locSelectDate = "1,2,3,4,5">
	<cfloop from="1" to="10" index="i">

		<cfloop from="1" to="#ListLen(locSelectDate)#" index="j">
			<cfset locSelectDay= ListGetAt(locSelectDate,j)>
			<cfset locDaySelect = DayOfWeek(locInitDate)-1>
			#i#:#locSelectDay#--#locDaySelect#<br>
			<cfif  locSelectDay eq locDaySelect>
				<cfset locInitDate = locInitDate>
				<cfbreak>
			<cfelse>
				<cfset locInitDate = DateAdd('d',1,locInitDate)>
				<cfcontinue>
			</cfif>

		</cfloop>
		Day#i#:#locInitDate#<br>
	</cfloop>

</cfoutput>

Open in new window

0
 
_agx_Commented:
>> and so on until 18 dates are fullfilled

Not sure how you're defining "fullfilled". 18 Mondays OR Wednesday's or 18 of both?

What's the last 2 dates you expect?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
well 18 in total. Mondays OR Wednesday's it just an example , it could have more days a week like Mondays OR Wednesday's, sunday. Or from Monday to Friday
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
erikTsomikSystem Architect, CF programmer Author Commented:
I am running this code but only get 9 records when I am looping over 10 times

<cfscript>
		  function Mydayofweek(date,day_1)
		  {
		      return (((DayOfWeek(date) + (7 -day_1)) MOD 7));
		  }
	</cfscript>
	<cfset selectDays = '1,2,3,5'>
	<cfset startDate = '5/1/2015'>
	<cfset locLen = ListLen(selectDays)>
<cfset endDate = DateAdd('d',10+locLen,'5/1/2015')>
<Cfset mydates = ''>

<cfset MyWeekFirstDay = 1><!---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 />">
		#mydates#<br>

	  </cfif>
	</cfloop>

Open in new window

0
 
_agx_Commented:
There's better ways to do it, but a basic loop would work:

<!--- assumes list of days cannot be empty --->
<cfset selectedDays = "2,4">
<cfset maxDates = 18>
<cfset startDate = createDate(2015,5,1)>
<cfset endDate = dateAdd("ww", (maxDates / listLen(selectedDays) + 1), startDate)>

<cfset dates = []>
<cfloop from="#startDate#" to="#endDate#" step="1" index="currDate">
	<cfif listFind(selectedDays, dayOfWeek(currDate))>
		<cfset arrayAppend(dates, dateFormat(currDate))>
	</cfif>
	
	<cfif arrayLen(dates) gte maxDates>
		<cfbreak>
	</cfif>
</cfloop>


<cfdump var="#dates#">

Open in new window

0
 
erikTsomikSystem Architect, CF programmer Author Commented:
it gives the correct number , however I am missing dates

for example if I use 5/1/2015

and set <cfset selectedDays = "1,2,5">

I do not get 5/1/2015 and I should beucase 5/1/2015 falls on friday
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
Found the issue . IN days of week I need to say -1

<cfif listFind(selectedDays, dayOfWeek(currDate)-1)>
0
 
_agx_Commented:
It shouldn't pick up Friday, because that's day of week 6.
0
 
_agx_Commented:
Ohh... if you're using different day of week than CF, then yes you need to adjust the logic. My example was written for CF's days of week, ie:

       1- Sunday, 2-Monday,....,7-Saturday.
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
but if I do -1 it will be 5 ad it works for me.

Is there a way I can show 2 date Slots per day . Same approach but instead of doing session per day display 2 sessions per day. so it will be 5/1/2015,5/1/2015 and them Monday 5/4/2015,5/4/2015 and so on
0
 
_agx_Commented:
Not sure I follow.  Once you have an array of dates, you can do whatever you need. Nothing to stop you from displaying each value twice in your output.
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
In one state the user take 1 class of 3 hours hours long (10 session in total) , in another state the user's class broken down in to 2 sessions (on the same date) 1 hour 30 minutes each plus the second session has 15 minutes break (18 sessions in total). That why in some cases I need that array to show duplicate date
0
 
_agx_Commented:
(Edit) If you really need to, you could always create populate the array with multiple values. However, that really sounds like something that should be handled during output, rather than when you generate the array of dates.  Is there some reason you can't just cfoutput the date value twice?
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
Because I am in the loop of number of sessions
<cfloop from="1" to="18" index="i">
and here i call the array to populate the dates dates[i]
</cfloop>

Open in new window

0
 
_agx_Commented:
Looping doesn't stop you from appending multiple values to the array, instead of just one OR creating an array of structures, which is more readable:

     ...
     <cfset value = { firstDate=..., secondDate=...}>
     <cfset arrayAppend(dates, value)>
     ...

Though I'm still not sure why you can't just display the date twice when you output...
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I came up with this
<cfloop index="ii" from="1" to="18">
 <cfscript>

						   		 if (ii mod 2 == 1){
						   		 	startTime = CreateODBCTime("#arguments.startTime#");
						   		 	endTime = CreateODBCTime(DateAdd('n',100,startTime));
						   		 }else{
									startTime = CreateODBCTime(DateAdd('n',15,endTime));
						   		 	endTime = CreateODBCTime(DateAdd('n',100,startTime));
						   		 }

								
						   </cfscript>

<cfif (ii mod 2 eq 1)>
								<cfset sessionStart = dateformat(dates[ii], 'YYYY-mm-dd') & " " & timeformat(startTime, 'hh:mm:ss tt')>
                				<cfset sessionEnd = dateformat(dates[ii], 'YYYY-mm-dd') & " " & timeformat(endTime, 'hh:mm:ss tt')>
							<cfelse>
								<cfset sessionStart = dateformat(dates[ii-1], 'YYYY-mm-dd') & " " & timeformat(startTime, 'hh:mm:ss tt')>
                				<cfset sessionEnd = dateformat(dates[ii-1], 'YYYY-mm-dd') & " " & timeformat(endTime, 'hh:mm:ss tt')>
							</cfif>
</cfloop>

Open in new window

0
 
_agx_Commented:
>> so it will be 5/1/2015,5/1/2015 and them Monday 5/4/2015,5/4/2015 and so on

That looks a lot different than what you described earlier.  The code above seems to be using two different dates: dates[ii] and dates[ii-1]

Honestly, I'm not really following what you're trying to do :)
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
I am just trying to display pairs.

So the first pair would be 5/1/2015 from 10:00 till 12:00pm
The second will be 5/1/2015 from 12:15p.m till 2:00pm


And then I am moving to the next date.
so it will be 5/2/2015 from 10:00 till 12:00pm and 5/2/2015 from 12:15p.m till 2:00pm

and so on

In my array I have 5/1/2015 ,

array
1       01-May-15
2       04-May-15
3       05-May-15
4       06-May-15
5       07-May-15
6       08-May-15
7       11-May-15
8       12-May-15
9       13-May-15
10       14-May-15
11       15-May-15
12       18-May-15
13       19-May-15
14       20-May-15
15       21-May-15
16       22-May-15
17       25-May-15
18       26-May-15
0
 
erikTsomikSystem Architect, CF programmer Author Commented:
you see I have 18 elements in the array. in reality i should have 18 but only untill 13-May-2015 because each date will be duplicated and make 18 elements in total
0
 
_agx_Commented:
Edit

>> if (ii mod 2 == 1){

Either I'm missing something obvious or there's something else going on you're not telling us. I don't see the the point of the cfif.  Why can't you just generate both times within the same iteration of the loop. Then do whatever you need to do with the two time values: store them in a structure, output them, etc...

<cfloop ...>
    ... 
   <cfset startTime1 =  .... do stuff ...>
   <cfset endTime1 = .... do stuff ...>
   <cfset startTime2 =  .... do stuff ...>
   <cfset endTime2 =  .... do stuff ...>

    ... do stuff with date and times ....
</cfloop>

Open in new window

0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now