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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help completing a date function

Status
Not open for further replies.

Sdaddy

MIS
Nov 6, 2002
41
US
I need to complete this function

Function WeekOfMonth(ByVal datDate As Date) As String
WeekOfMonth = "Week " & DatePart("ww", datDate) - _
DatePart("ww", Month(datDate) & _
"/1/" & Year(datDate)) + 1
End Function
(thanks vbslammer)
This function gives me the week of a particular month. However, if the week ends in a different month, it gives me week 1 of that month rather than week 5 or 6 from the current month. For instance, at the end of the last october i need 10/27-10/31 to show as week 5 of october rather than week 1 of November. I will be forever indebted to the person that helps.

[cannon][machinegun][auto][flush]
 
Copying your code to a module then testing it from the
debug window results in:

? weekOfMonth(#10/27/02#)
Week 5
? WeekOfMonth(#10/28/02#)
Week 5
? WeekOfMonth(#10/29/02#)
Week 5
? weekOfMonth(#10/30/02#)
Week 5
? WeekOfMonth(#10/31/02#)
Week 5
? WeekOfMonth(#11/01/02#)
Week 1

So, it doesn't appear that the code produces "Week 1", as
stated in your post, for any of the dates listed in October

If, however, your weeks were based on the weekday of the
the first day of 10/02, the results would be different.

? weekOfMonth2(#10/27/02#)
Week 4
? WeekOfMonth2(#10/28/02#)
Week 4
? WeekOfMonth2(#10/29/02#)
Week 5
? weekOfMonth2(#10/30/02#)
Week 5
? WeekOfMonth2(#10/31/02#)
Week 5
? WeekOfMonth2(#11/01/02#)
Week 1

And the code for WeekOfMonth2() might look like this:
Code:
Function WeekOfMonth2(ByVal myDate As Date) As String
Dim dteFirst As Variant, intFirst As Integer
Dim intMyWeek As Integer, intFirstWeek As Integer

dteFirst = DateValue(Month(myDate) & "/1/" & Year(myDate))
intFirst = WeekDay(dteFirst)

intMyWeek = DatePart("ww", myDate, intFirst)
intFirstWeek = DatePart("ww", dteFirst, intFirst)
WeekOfMonth2 = "Week " & intMyWeek - intFirstWeek + 1

End Function
Try stepping through it to see the differences.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top