Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statements to test for third Wed of the month, also third Thurs.

Status
Not open for further replies.

dgr7

IS-IT--Management
Dec 29, 2006
43
US
hello,
what would be the VB6 code for an if statement that would check for if it's the third wednesday of the month and execute some code based on that. Also would would be the similar code for if it's the third thursday of the month?
thanks in advance,
david
 
Code:
Public Function ThirdWed(myDate As Date) As Boolean
ThirdWed = False
Select Case Day(myDate)
Case 15 To 21
If Weekday(myDate) = vbWednesday Then ThirdWed = True
End Select
End Function
The code for Thursday is left as an exercise!

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
You can create a simple Function where you can do that:

Pass the day (today’s date maybe?) to this function, and check what day of the week was the first day of the month. Every month has the 1st, so that’s no problem.

Hint: VB 6 build in Weekday function is very handy,
It returns:
1 for Sunday
2 for Monday
3 for Tuesday
4 for Wednesday
5 for Thursday
6 for Friday
7 for Saturday

If not 4 (Wednesday) you can keep adding days to your first of the month day (check DateAdd function in VB) to find very first Wednesday of the month.

With the same DateAdd function you can now add 3 weeks to the first Wednesday of the month, and now you just need to compare the dates: if the same, you have third Wednesday, if not – then you do not.

I have a piece of code that works, but I will let you figure it out. Unless it is NOT a homework. :)



Have fun.

---- Andy
 
I've got a one-liner that will tell you if whether any given date is the xth particular day of the particular month...

 
>one-liner

Does this work?
___
[tt]
Function DateTest(dt As Date, DayofWeek As VbDayOfWeek, WeekDayofMonth As Integer) As Boolean
DateTest = Weekday(dt) = DayofWeek And (Day(dt) + 6) \ 7 = WeekDayofMonth
End Function
...
Msgbox DateTest(#3/31/2007#, vbSaturday, 5)[/tt]
 
Seems to. And seems to get the same results as mine:

Public Function IsWhatDay(ByVal TestDate As Date, lCount As Long, TestWeekDay As VbDayOfWeek) As Boolean
IsWhatDay = (Day(TestDate) = (2 + 7 * lCount) - Weekday(Format(TestDate, "1 mmm yyyy"), TestWeekDay))
End Function
 
thanks for everyone's input. With your help, I came up with this test code that worked well as I changed the pc's calendar to the third wed, third thurs and some other days.
Code:
If Weekday(Date) = vbWednesday And Day(Date) >= 15 And Day(Date) <= 21 Then
    MsgBox "Today is the third Wednesday of the month"
Else
    If Weekday(Date) = vbThursday And Day(Date) >= 15 And Day(Date) <= 21 Then
        MsgBox "Today is the third Thursday of the month"
    Else
        MsgBox "today is the " & Weekday(Date) & " / " & Day(Date) & " day of " & Month(Date) & " " & Year(Date)
    End If
End If
 


I seem to only always get a correct result from Hypetia's solution.
 
You're quite right - there's a minor modification that needs to be applied to mine ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top