Results 1 to 3 of 3

Thread: [RESOLVED] How to find Week Of Year ?

  1. #1

    Thread Starter
    Member Anwin's Avatar
    Join Date
    May 2005
    Posts
    43

    Resolved [RESOLVED] How to find Week Of Year ?

    Hi

    Creating an ordinary windows application, I can use this code to find the week of year from a MonthCalendar control:

    TextBox1.Text = DatePart(DateInterval.WeekOfYear, MonthCalendar1.SelectionStart)

    However, using the same code in my Device app. for pocket pc, won't work
    Hoping for an easy solution

    Anwin
    Last edited by Anwin; Nov 3rd, 2009 at 03:49 AM.

  2. #2

    Thread Starter
    Member Anwin's Avatar
    Join Date
    May 2005
    Posts
    43

    Re: How to find Week Of Year ?

    I couldn't find a answer to my problem, so I tried to make my own code for how to find a week of year from a given date.

    I'm from Norway and we use this standards:
    First day of week:
    Monday (complies with ISO standard 8601, section 3.17)
    First week of year:
    Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)

    After this standards this function will return the week (as integer) when calling the function with a given date (as date).

    It might not be the easiest code, but as far as I can tell it's working.

    VB.Net Code:

    Function FindWeek(ByVal dtmToday As Date) As Integer
    Dim intThisYear, intThisMonth, x As Integer
    Dim dtmFirstDayOfWeek, FirstDayOfYear As Date
    Dim MytimeSpan As TimeSpan

    ' Find first day of week

    Do While dtmToday.DayOfWeek <> 1
    intThisYear = dtmToday.Year
    intThisMonth = dtmToday.Month
    MytimeSpan = dtmToday.Subtract(CDate(1 & "." & intThisMonth & "." & intThisYear))
    x = MytimeSpan.Days.ToString
    If x = 0 Then
    If intThisMonth = 1 Then
    intThisMonth = 13
    intThisYear = intThisYear - 1
    End If
    x = DateTime.DaysInMonth(intThisYear, intThisMonth - 1)
    dtmToday = CDate(x & "." & intThisMonth - 1 & "." & intThisYear)
    Else
    dtmToday = CDate(x & "." & intThisMonth & "." & intThisYear)
    End If
    Loop
    dtmFirstDayOfWeek = dtmToday

    'Find week of year from first day of week
    FirstDayOfYear = CDate("01.01." & dtmFirstDayOfWeek.ToString("yyyy"))
    x = 0
    If FirstDayOfYear.DayOfWeek = 5 Or FirstDayOfYear.DayOfWeek = 6 Or FirstDayOfYear.DayOfWeek = 0 Then
    For x = 0 To dtmFirstDayOfWeek.DayOfYear + 1
    Next x
    x = (x / 7)
    Else
    For x = 1 To dtmFirstDayOfWeek.DayOfYear
    Next x
    x = (x / 7) + 1
    If dtmFirstDayOfWeek.Month = 12 Then
    If dtmFirstDayOfWeek.Day = 29 Then x = 1
    If dtmFirstDayOfWeek.Day = 30 Then x = 1
    If dtmFirstDayOfWeek.Day = 31 Then x = 1
    End If
    End If
    Return x
    End Function

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How to find Week Of Year ?

    I was lost in space when you originally posted, or I would have suggested something along those lines. The CF had to be stripped down to fit in the memory constraints of a PDA, and what was left out could be awkward. You just have to find a way around it, and it looks like you did.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width