Ideas on what's missing?

# Thread: Ideas on what's missing?

1. Member
Join Date
Dec 1969
Posts
99

## Ideas on what's missing?

I am trying to work this to figure the second tuesdays in every month. I got the idea for this code, from a snibbit at developer.irt.org. I am trying to take apart the code to see what the math is actually doing, but I am getting no where fast. If you are familiar with anything I&#039;ve said, please let me in on what I am missing.<BR><BR>Thanks.<BR><BR>&#060;%<BR><BR>&#03 9;get the current year, and year plus 1,2...etc<BR>dim baseYear, Year1, Year2, Year3, Year4, Year5<BR><BR>baseYear = year(date())<BR>year1 = baseYear-2<BR>year2 = baseYear-1<BR>year3 = baseYear<BR>year4 = baseYear+1<BR>year5 = baseYear+2<BR>%&#062;<BR><BR>&#060;HTML&#062;<BR>& #060;HEAD&#062;<BR><BR>&#060;SCRIPT LANGUAGE="JavaScript"&#062;&#060;!--<BR>function getMonth(){<BR> var Month = form1.month.value;<BR> var Year = form1.year.value;<BR><BR> var Total = (Month+&#039;/&#039;+NthDay(2,3,Month,Year)+&#039;/&#039;+Year);<BR> form2.text1.value = Total;<BR> }<BR><BR>function DayOfWeek(day,month,year) {<BR> var a = Math.floor((14 - month)/12);<BR> var y = year - a;<BR> var m = month + 12*a - 2;<BR> var d = (day + y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) + Math.floor((31*m)/12)) % 7;<BR> return d+1;<BR>}<BR><BR><BR>function NthDay(nth,weekday,month,year) {<BR> <BR> return (nth-1)*7 + 1 + (7 + weekday - DayOfWeek((nth-1)*7 + 1,month,year))%7;<BR> <BR>}<BR>//--&#062;&#060;/SCRIPT&#062;<BR><BR>&#060;/HEAD&#062;<BR><BR>&#060;BODY&#062;<BR>&#060;form name=form1&#062;<BR>&#060;table width="50%" border="0"&#062;<BR>&#060;tr&#062;<BR> &#060;td width="30%"&#062;&#060;font size="2" color="#000000"&#062;Month:&#060;/font&#062;&#060;/td&#062;<BR> &#060;td width="70%"&#062;&#060;SELECT id=month name=month&#062;<BR><BR> &#060;OPTION value=1&#062;January&#060;/OPTION&#062;<BR> &#060;OPTION value=2&#062;February&#060;/OPTION&#062;<BR> &#060;OPTION value=3&#062;March&#060;/OPTION&#062;<BR> &#060;OPTION value=4&#062;April&#060;/OPTION&#062;<BR> &#060;OPTION value=5&#062;May&#060;/OPTION&#062;<BR> &#060;OPTION value=6&#062;June&#060;/OPTION&#062;<BR> &#060;OPTION value=7&#062;July&#060;/OPTION&#062;<BR> &#060;OPTION value=8&#062;August&#060;/OPTION&#062;<BR> &#060;OPTION value=9&#062;September&#060;/OPTION&#062;<BR> &#060;OPTION value=10&#062;October&#060;/OPTION&#062;<BR> &#060;OPTION value=11&#062;November&#060;/OPTION&#062;<BR> &#060;OPTION value=12&#062;December&#060;/OPTION&#062;<BR><BR> &#060;/SELECT&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr&#062;<BR> &#060;td width="30%"&#062;&#060;font size="2" color="#000000"&#062;Year:&#060;/font&#062;&#060;/td&#062;<BR> &#060;td width="70%"&#062;&#060;SELECT id=year name=year onchange="getMonth();"&#062;<BR><BR> &#060;OPTION value=&#060;%=year1%&#062;&#062;&#060;%=year1%&#06 2;&#060;/OPTION&#062;<BR> &#060;OPTION value=&#060;%=year2%&#062;&#062;&#060;%=year2%&#06 2;&#060;/OPTION&#062;<BR> &#060;OPTION value=&#060;%=year3%&#062;&#062;&#060;%=year3%&#06 2;&#060;/OPTION&#062;<BR> &#060;OPTION value=&#060;%=year4%&#062;&#062;&#060;%=year4%&#06 2;&#060;/OPTION&#062;<BR> &#060;OPTION value=&#060;%=year5%&#062;&#062;&#060;%=year5%&#06 2;&#060;/OPTION&#062;<BR><BR>&#060;/SELECT&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/form&#062;<BR>&#060;form name=form2&#062;<BR>&#060;tr&#062;<BR> &#060;td width="30%"&#062;&#060;font size="2" color="#000000"&#062;Date:&#060;/font&#062;&#060;/td&#062;<BR> &#060;td width="70%"&#062;&#060;INPUT type="text" id=text1 name=text1&#062;&#060;/td&#062;<BR>&#060;/tr&#062;<BR>&#060;/table&#062;<BR>&#060;/form&#062;<BR><BR>&#060;/BODY&#062;<BR>&#060;/HTML&#062;<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## day of week is built in...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthgetday.asp<BR><BR>And your month values are off by one. JS uses 0-11 for the months, not 1-12.<BR><BR>Other than that, there *has* to be a simpler way to do this.<BR><BR>Let&#039;s see... Okay, get a JS date object set to the first day of the month you are interested in. Figure out what day of the week that is and adjust the day forwards (if needed) to the first Tuesday and then adjust forwards by 7 times number of weeks.<BR><BR>Here...a demo...it works:<BR><BR>**** NthDay.html ****<BR><BR>&#060;HTML&#062;&#060;BODY&#062;<BR>&# 060;SCRIPT&#062;<BR>function getNthDay( theYear, theMonth, desiredDayOfWeek, desiredWeek )<BR>{<BR>&nbsp; &nbsp; var first = new Date( theYear, theMonth, 1 );<BR>&nbsp; &nbsp; var firstDay = desiredDayOfWeek + 1 - first.getDay() <BR>&nbsp; &nbsp; if ( firstDay &#060; 0 ) firstDay += 7;<BR>&nbsp; &nbsp; return new Date( theYear, theMonth, firstDay + (desiredWeek-1) * 7 );<BR>}<BR><BR>document.write( "&#060;P&#062;3rd Tuesday of Feb, 2002: " + getNthDay(2002,1,2,3).toString() );<BR><BR>document.write( "&#060;P&#062;3rd Sunday of Aug, 1944: " + getNthDay(1944,7,0,3).toString() );<BR><BR>&#060;/SCRIPT&#062;<BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR><BR><BR>***********<BR><BR>I think that does it? Try it out?<BR><BR>And why the VBS/ASP code in there??? You can get the year just as easily in JS code and build up the &#060;SELECT&#062; on year there.<BR><BR>

3. Member
Join Date
Dec 1969
Posts
99

## the only correction I have...

if ( firstDay &#060; 0 ) firstDay += 7;<BR><BR>should be:<BR><BR>if ( firstDay &#060; 2 ) firstDay += 7;<BR><BR>Thanks for the input!<BR>

4. Member
Join Date
Dec 1969
Posts
99

## wow, I stink. quick question BILL

BILL, <BR>this code works, kinda. I was wrong in changing 0 to 2. Here is the complete code:<BR><BR>&#060;HTML&#062;<BR>&#060;HEAD&#062;< BR><BR>&#060;SCRIPT LANGUAGE="JavaScript"&#062;&#060;!--<BR><BR>function getStarted(){<BR> var Month = form1.month.value;<BR> var Year = form1.year.value;<BR> var Day = form1.day.value;<BR> var numDay = eval(Day);<BR> <BR> var fixedMonth = eval(Month)+1;<BR> <BR> var Total = (fixedMonth+&#039;/&#039;+getNthDay(Year,Month,numDay,2)+&#039;/&#039;+Year);<BR> <BR> var monthsofyear = new Array(&#039;January&#039;,&#039;February&#039;,&#0 39;March&#039;,&#039;April&#039;,&#039;May&#039;,& #039;June&#039;,&#039;July&#039;,&#039;August&#039 ;,&#039;September&#039;,&#039;October&#039;,&#039; November&#039;,&#039;December&#039;);<BR> <BR> form2.meetingDate.value = Total;<BR> form2.meetingMonth.value = monthsofyear[Month];<BR> form2.meetingYear.value = Year;<BR> form2.meetingDay.value = numDay;<BR> }<BR><BR>function getNthDay( theYear, theMonth, desiredDayOfWeek, desiredWeek ) <BR>{ <BR> var first = new Date( theYear, theMonth, 1 );<BR> <BR> var firstDay = desiredDayOfWeek + 1 - first.getDay() <BR> //document.write(firstDay);<BR> if ( firstDay &#060;= 0 ) firstDay += 7; <BR> //document.write(firstDay);<BR> <BR> return new Date( theYear, theMonth, firstDay + (desiredWeek-1) * 7 ).getDate(); <BR>} <BR><BR>//--&#062;&#060;/SCRIPT&#062;<BR><BR>&#060;/HEAD&#062;<BR><BR>&#060;BODY&#062;<BR>&#060;form name=form1&#062;<BR>&#060;table width="17%" border="1"&#062;<BR>&#060;tr&#062;<BR> &#060;td width="12%"&#062;&#060;font size="2" color="#000000"&#062;Day:&#060;/font&#062;&#060;/td&#062;<BR> &#060;td width="88%"&#062;&#060;SELECT id=day name=day&#062;<BR><BR> &#060;OPTION selected&#062;- -&#060;/OPTION&#062;<BR> &#060;OPTION value=2&#062;Tuesday&#060;/OPTION&#062;<BR> &#060;OPTION value=3&#062;Wednesday&#060;/OPTION&#062;<BR> <BR> &#060;/SELECT&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr&#062;<BR>&#060;tr&#062;<BR> &#060;td width="12%"&#062;&#060;font size="2" color="#000000"&#062;Month:&#060;/font&#062;&#060;/td&#062;<BR> &#060;td width="88%"&#062;&#060;SELECT id=month name=month&#062;<BR><BR> &#060;OPTION selected&#062;- -&#060;/OPTION&#062;<BR> &#060;OPTION value=0&#062;January&#060;/OPTION&#062;<BR> &#060;OPTION value=1&#062;February&#060;/OPTION&#062;<BR> &#060;OPTION value=2&#062;March&#060;/OPTION&#062;<BR> &#060;OPTION value=3&#062;April&#060;/OPTION&#062;<BR> &#060;OPTION value=4&#062;May&#060;/OPTION&#062;<BR> &#060;OPTION value=5&#062;June&#060;/OPTION&#062;<BR> &#060;OPTION value=6&#062;July&#060;/OPTION&#062;<BR> &#060;OPTION value=7&#062;August&#060;/OPTION&#062;<BR> &#060;OPTION value=8&#062;September&#060;/OPTION&#062;<BR> &#060;OPTION value=9&#062;October&#060;/OPTION&#062;<BR> &#060;OPTION value=10&#062;November&#060;/OPTION&#062;<BR> &#060;OPTION value=11&#062;December&#060;/OPTION&#062;<BR><BR> &#060;/SELECT&#062;&#060;/td&#062;<BR> &#060;/tr&#062;<BR> &#060;tr&#062;<BR> &#060;td width="12%"&#062;&#060;font size="2" color="#000000"&#062;Year:&#060;/font&#062;&#060;/td&#062;<BR> &#060;td width="88%"&#062;&#060;select name="year"&#062;<BR>&#060;option selected&#062;- -&#060;/option&#062;<BR>&#060;%<BR>&#039;populate year dropdown<BR>intYear = Year(date())<BR><BR>for intCount = (intYear) to (intYear + 2)<BR> response.write "&#060;Option value=""" & intCount & """&#062;" & _<BR> intCount & "&#060;/option&#062;"<BR>next<BR>%&#062;<BR>&#060;/select&#062;<BR>&#060;INPUT type="button" value="go" id=button1 name=button1 onclick="getStarted();"&#062;<BR>&#060;/td&#062;<BR> &#060;/tr&#062;<BR>&#060;/form&#062;<BR>&#060;form name=form2&#062;<BR>&#060;tr&#062;<BR> &#060;td width="12%"&#062;&#060;font size="2" color="#000000"&#062;Date:&#060;/font&#062;&#060;/td&#062;<BR> &#060;td width="88%"&#062;&#060;INPUT type="text" id="meetingDate" name="meetingDate" size="13"&#062;&#060;/td&#062;<BR>&#060;/tr&#062;<BR>&#060;/table&#062;<BR><BR>&#060;!--for placment in the db--&#062;<BR>&#060;INPUT type="text" id="meetingDay" name="meetingDay"&#062;<BR>&#060;INPUT type="hidden" id="meetingMonth" name="meetingMonth"&#062;<BR>&#060;INPUT type="hidden" id="meetingYear" name="meetingYear"&#062;<BR><BR>&#060;/form&#062;<BR>&#060;/BODY&#062;<BR>&#060;/HTML&#062;<BR><BR><BR>It works up until May of 2002. It sets the second Tuesday as the 8th. I&#039;m sick of fiddling.

5. Member
Join Date
Dec 1969
Posts
99

## WOW I REALLY STINK<eop>

n/m it works fine, it&#039;s my calendar that&#039;s broken. :)

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Good catch on the <= vs < 0 (eom).

.<BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•