Results 1 to 4 of 4

Thread: [2005] Getting current week number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    69

    Question [2005] Getting current week number

    Hi all

    First of all I must apologize for brining this subject up yet again on these forums. I have searched for soluions and I also thought i found it.
    However it seems to make some kind of miscalculation or something..
    Here's one of the solutions I found in my search.
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         MessageBox.Show(Me.GetMonthWeekNumber(Me.DateTimePicker1.Value).ToString())
    5.  
    6.     End Sub
    7.  
    8.     Private Function GetMonthWeekNumber(ByVal [date] As Date) As Integer
    9.         Dim cal As Globalization.Calendar = Globalization.CultureInfo.CurrentCulture.Calendar
    10.  
    11.         Dim monthStartWeek As Integer = cal.GetWeekOfYear(New Date([date].Year, _
    12.                                                                    [date].Month, _
    13.                                                                    1), _
    14.                                                           Globalization.CalendarWeekRule.FirstDay, _
    15.                                                           DayOfWeek.Monday)
    16.         Dim currentWeek As Integer = cal.GetWeekOfYear([date], _
    17.                                                        Globalization.CalendarWeekRule.FirstDay, _
    18.                                                        DayOfWeek.Monday)
    19.  
    20.         Dim monthWeekNumber As Integer = currentWeek - monthStartWeek + 1
    21.  
    22.         Return monthWeekNumber
    23.     End Function
    24.  
    25. End Class
    But when i compared the result with what i found on MS Outlook it didnt seem to match. Outlook, as does my program, use Monday as first day of week. But if you select the date 01.01.2006 in the DateTimePicker it will say that the weeknumber is 1. Outlook however claims it to be week #52.
    (01.01.2006 is a sunday). This means that my program, using the code above, will claim that there are 53 weeks in this year..
    Is there any other method I can use that will display the correct week number of a selected date?
    2B || !2B

  2. #2
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] Getting current week number

    Look at JMC's approach in post #11
    http://www.vbforums.com/showthread.p...ht=week+number

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    69

    Re: [2005] Getting current week number

    I've tried with the code i found on that page (its the same as my original post), but it gave the same result. 01.01.2006 gives me week #1.
    2B || !2B

  4. #4
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    Re: [2005] Getting current week number

    hi,

    Depending on how you count weeks(depending on where you live), the week number 1 can be:

    the 1st of January
    or
    the first monday of January,

    so if you want to have the second option, just check if 1st of january is monday if not substract 1 to the week number you have (be careful then all the days of january not included on the first week of year will be included on the last week of past year)

    Hope this helps
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

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