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

How do I convert the Week No into a Date Range 1

Status
Not open for further replies.

Razor1

IS-IT--Management
Nov 21, 2002
81
US
In my project I have a week no. I need to convert that into the date range for that week.

Any ideas on how to accomplish this?

Thanks in advance for any help .

Razor


Razor1
 
Week Number" is a bit of an ambiguous term.

It can be interpreted as seven-day intervals beginning on January 1st OR it can be interpreted as the number of Sundays + 1 after January 1st.

Which is the interpretation of your "Week Number"?
 
I am using Monday's as the start of the week.

Razor1
 
OK. Then I assume that the number of Mondays + 1 after January 1st is your Week Number value.

If So ...

Code:
Public Function GetWeekStart(WeekNo As Integer, YearNo As Integer, _
                             Optional WeekStartDay As Integer = vbSunday) As Date
    Dim n                           As Integer
    Dim SecondWeekStart             As Date
    If WeekNo > 0 Then
        For n = 2 To 8
            SecondWeekStart = DateSerial(YearNo, 1, n)
            If Weekday(SecondWeekStart) = WeekStartDay Then Exit For
        Next
        GetWeekStart = SecondWeekStart + ((WeekNo - 1) * 7)
    Else
        SecondWeekStart = DateSerial(YearNo, 1, 1)
        GetWeekStart = SecondWeekStart
    End If

End Function

And call it with
Code:
Dim StartOfWeek As Date
Dim EndOfWeek   As Date
StartOfWeek = GetWeekStart(WeekNo, YearNo, vbMonday)
EndOfWeek = StartOfWeek + 7
 
Thanks. That worked.

Have a star.


Razor1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top