Results 1 to 12 of 12

Thread: [RESOLVED] Number of the week of the current month -VBE

  1. #1

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Resolved [RESOLVED] Number of the week of the current month -VBE

    Hi,
    How Can we caliculate the number of the week of the current month.
    thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Number of the week of the current month -VBE

    Are you saying that you want the week number within the month of the current date? If so then we'd need more information. Would you consider the first week of the month to be the first seven days, the first period ending in a Saturday, the first period ending in a Sunday or something else? Or is that not what you want at all? Remember that posts don't have to be kept to one line. Don't be afraid to elaborate or provide an example to clarify your request. Don't assume that brief is good if it doesn't fully describe your circumstances.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Number of the week of the current month -VBE

    Jmcilhinney has a good point ,because in different part of the country the starting day of the week is different.

    I think you should look at the System.DateAndTime and System.TimeSpan namespaces might find your answer in there.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  4. #4

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: Number of the week of the current month -VBE

    Yes i want the week number within the month of the current date.
    For example if the customer selected today date in datepicker,
    then i need to give the number 3.because we are in third week of this month
    thanks

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Number of the week of the current month -VBE

    *sigh* And what constitutes the first week of a month? I didn't just ask that before for my health. By my calendar, on which weeks start on a Monday, we are in the fourth week September because the first week was only three days. If you're saying that we're in the third week then that would imply you just want to start the first week at the first of the month. If that's the case then if today is the 19th day of the month and there are 7 days in a week, I can think of a very easy way to get the week number from that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: Number of the week of the current month -VBE

    By my calender week started on friday,so i am thinking we are in week number 4
    I tried with this coding
    VB Code:
    1. Dim First As DateTime = New DateTime(Now.Year, Now.Month, 1)
    2.         With System.Globalization.CultureInfo.CurrentCulture
    3.             Dim WeekOfNow As Integer = .Calendar.GetWeekOfYear(Now, Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)
    4.             Dim WeekOfFirst As Integer = .Calendar.GetWeekOfYear(First, Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)
    5.                  Messagebox (1 + WeekOfNow - WeekOfFirst)
    6.         End With

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Number of the week of the current month -VBE

    And did that work? I used this on my system and it worked perfectly:
    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
    Looks pretty much the same as what you used except that Monday is the first day of the week on my system.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: Number of the week of the current month -VBE

    ya,both are working same

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Number of the week of the current month -VBE

    If your problem is resolved it's a good idea to say so. You said you "tried" that code which implied to me that it failed. You should resolve your thread from the Thread Tools menu if your question has been answered.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: Number of the week of the current month -VBE

    can we generalize the thing,like for any of the day,monday or sunday etc

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Number of the week of the current month -VBE

    The sensible thing to do would be to use all the current system settings:
    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 culture As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
    10.         Dim calendar As Globalization.Calendar = culture.Calendar
    11.         Dim rule As Globalization.CalendarWeekRule = culture.DateTimeFormat.CalendarWeekRule
    12.         Dim firstDayOfWeek As DayOfWeek = culture.DateTimeFormat.FirstDayOfWeek
    13.  
    14.         Dim monthStartWeek As Integer = calendar.GetWeekOfYear(New Date([date].Year, _
    15.                                                                         [date].Month, _
    16.                                                                         1), _
    17.                                                                rule, _
    18.                                                                firstDayOfWeek)
    19.         Dim currentWeek As Integer = calendar.GetWeekOfYear([date], _
    20.                                                             rule, _
    21.                                                             firstDayOfWeek)
    22.  
    23.         Dim monthWeekNumber As Integer = currentWeek - monthStartWeek + 1
    24.  
    25.         Return monthWeekNumber
    26.     End Function
    27.  
    28. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: [RESOLVED] Number of the week of the current month -VBE

    using system settings is a good point
    Thanks

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