The challenge was ....

# Thread: The challenge was ....

1. Senior Member
Join Date
Dec 1969
Posts
11,247

## The challenge was ....

to find the first Thursday of the month.<BR>Well I thought about it and have created a Function to find the first time a day occurs in any month.<BR>Would like some feed back pls<BR><BR>&#060;%<BR>Function FirstofMonth(daypart, Mon, Years)<BR> Select Case LCase(daypart)<BR> Case "sunday"<BR> datep = VBSunday<BR> case "monday"<BR> datep = VBMonday<BR> Case "tuesday"<BR> datep =VBTuesday<BR> Case "wednesday"<BR> datep = VBWednesday<BR> Case "thursday"<BR> datep = VBThursday<BR> Case "friday"<BR> datep = VBFriday<BR> Case "saturday"<BR> datep =VBSaturday<BR> End Select<BR><BR> firstdayofmonth = Datepart("w", Dateserial( Years, Mon, "1"))<BR> if firstdayofmonth = datep then<BR> FirstofMonth = Dateserial( Years, Mon, "1")<BR> elseif firstdayofmonth &#062; datep then<BR> FirstofMonth = Dateserial( Years, Mon, 8 - (firstdayofmonth - datep) )<BR> else<BR> FirstofMonth = Dateserial( Years, Mon, 1 + (firstdayofmonth - datep) )<BR> end if<BR>End Function<BR><BR>test = FirstofMonth("Thursday", 6, 2002)<BR>response.write "<BR>" & formatdatetime(test, vblongdate)<BR> %&#062;

2. Senior Member
Join Date
Dec 1969
Posts
2,225

## ** Wrong Forum *** <eop>

.

3. Senior Member
Join Date
Dec 1969
Posts
11,247

## Where then? (eop)

.

4. Senior Member
Join Date
Dec 1969
Posts
2,225

## Post to ASP Q & A...

that is where I would have put it.

5. Senior Member
Join Date
Dec 1969
Posts
7,686

## in ONE line

It&#039;s not readable, but it is in one line:<BR><BR>FUNCTION FirstOfMonth (theDay, theMonth, theYear)<BR> Response.Write dateadd("d",((CINT((theDay &#060; WEEKDAY( theMonth & "/01/" & theYear))) * 7 *-1) ) + (theDay-WEEKDAY( theMonth & "/01/" & theYear)),theMonth & "/01/" & theYear)<BR>END FUNCTION<BR><BR>Response.Write FirstOfMonth (vbSaturday,11,2002)<BR><BR><BR>(and I used the built in day constants)<BR>

6. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: in ONE line

Type mismatch: &#039;theDay&#039;

7. Senior Member
Join Date
Dec 1969
Posts
7,686

## RE: in ONE line

Are you sure? Works for me. Pay attention to the linebreaks caused by this board<BR><BR>FUNCTION FirstOfMonth (theDay, theMonth, theYear)<BR> Response.Write dateadd("d",((CINT((theDay &#060; WEEKDAY( theMonth & "/01/" & theYear))) * 7 *-1) ) + (theDay-WEEKDAY( theMonth & "/01/" & theYear)),theMonth & "/01/" & theYear)<BR>END FUNCTION<BR><BR>Response.Write FirstOfMonth (vbWednesday,11,2002)

8. Senior Member
Join Date
Dec 1969
Posts
7,686

## Err

Response.Write Dateadd [..] <BR><BR>should be<BR><BR>FirstOfMonth = [..] of course<BR><BR>and *-7 is of course the same as *7 * -1<BR><BR>FUNCTION F (d, m, y)<BR>F=DATEADD("d",((CINT((d &#060; WEEKDAY(m&"/01/"&y)))*-7))+(d-WEEKDAY(m&"/01/"&y)),m&"/01/"&y)<BR>END FUNCTION

9. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: Err

I concede. But you have to know that d is vbthursday or whatever!<BR>:-)

10. Senior Member
Join Date
Dec 1969
Posts
7,686

## RE: Err

How do you mean "How do I know that day is vbThursDay"? <BR>You pass it in when you call the function, don&#039;t you<BR><BR>Response.Write F (vbThursDay, 11, 2002)<BR><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
•