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!

DateTime question

Status
Not open for further replies.

henna

Technical User
Dec 18, 2002
23
0
0
NZ
Hi
Im trying to return the first and last date by week number and year.

week = 24
year = 2005

startdate of week?
finishdate of week?

Has anyone done anything similar or point me to the right direction
 
For year it is MyDate.Year
There is no WeekOfTheYear built into .Net so you have to use the function below.


Private Function WeekOfTheYear(ByVal d As DateTime, Optional ByVal StartWithMonday As Boolean = True) As Integer
Dim offset As Integer = 0
If StartWithMonday Then offset = -1
Dim d1 As Integer = Convert.ToInt32(Convert.ToDateTime("1/1/" & d.Year.ToString).AddDays(offset).DayOfWeek)
Dim ret As Integer = d.DayOfYear + d1 - 1
ret = Math.Floor(ret / 7) + 1
Return ret
End Function
 
I think the user was actually after the opposite (pass a week number and get back a start/end date). If so, something like this would work:
Code:
    Public Function GetStartEndDate(ByVal WeekNo As Integer) As String
        Dim dt As New DateTime(DateTime.Now.Year, 1, 1)
        Dim strStart As String
        Dim strEnd As String
        dt = dt.AddDays(((WeekNo - 1) * 7) - 1)
        dt = dt.AddDays(dt.DayOfWeek.Monday - dt.DayOfWeek)
        strStart = dt.ToShortDateString()
        strEnd = dt.AddDays(7 - 1).ToShortDateString()
        Return "Start: " & strStart & " End: " & strEnd
    End Function


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
You are a star.
Thanks a lot thats exactly what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top