Solved

coldfusion DayOfWeek

Posted on 2015-01-06
20
160 Views
Last Modified: 2015-01-07
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
Comment
Question by:erikTsomik
  • 11
  • 9
20 Comments
 
LVL 19

Author Comment

by:erikTsomik
ID: 40534282
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
 
LVL 52

Expert Comment

by:_agx_
ID: 40534297
>> 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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40534324
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40534427
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
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 40534428
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40534464
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40534475
Found the issue . IN days of week I need to say -1

<cfif listFind(selectedDays, dayOfWeek(currDate)-1)>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40534484
It shouldn't pick up Friday, because that's day of week 6.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40534495
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40534499
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 52

Expert Comment

by:_agx_
ID: 40534507
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40534980
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
 
LVL 52

Expert Comment

by:_agx_
ID: 40535612
(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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40535707
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
 
LVL 52

Expert Comment

by:_agx_
ID: 40535771
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40535802
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
 
LVL 52

Expert Comment

by:_agx_
ID: 40535888
>> 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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40535905
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
 
LVL 19

Author Comment

by:erikTsomik
ID: 40535908
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
 
LVL 52

Expert Comment

by:_agx_
ID: 40536007
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now