CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2006
    Posts
    449

    How do I manipulate with the weeks?

    I need to manipulate with the week number of the year. I have a situation like... I get the start date and the days (multiple days) of the week like Monday & Friday from the user and need to perform the action. Then from then on every alternate week I need to perform the same action. Hope am clear.

    Now what I am planning to do is... I want to have a integer variable and store the week number of the start date week to that variable. And then by manipulating with week numbers I can perform the actions. Now HOW DO I GET the week number stored to that integer variable. Please guide / help me. Thank you.

    Any alternate thoughts / suggestion to handle this situation are welcome.
    My environment is VS2005 / SQL / VB / Windowsapp.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How do I manipulate with the weeks?

    Got this from a very clever dude once..

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show(Me.GetMonthWeekNumber(DateTime.Today).ToString())
    
        End Sub
    
        Private Function GetMonthWeekNumber(ByVal [date] As Date) As Integer
            Dim culture As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
            Dim calendar As Globalization.Calendar = culture.Calendar
            Dim rule As Globalization.CalendarWeekRule = culture.DateTimeFormat.CalendarWeekRule
            Dim firstDayOfWeek As DayOfWeek = culture.DateTimeFormat.FirstDayOfWeek
    
            Dim monthStartWeek As Integer = calendar.GetWeekOfYear(New Date([date].Year, _
                                                                            [date].Month, _
                                                                            1), _
                                                                   rule, _
                                                                   firstDayOfWeek)
            Dim currentWeek As Integer = calendar.GetWeekOfYear([date], _
                                                                rule, _
                                                                firstDayOfWeek)
    
            Dim monthWeekNumber As Integer = currentWeek - monthStartWeek + 1
    
            Return monthWeekNumber
        End Function

  3. #3
    Join Date
    Oct 2006
    Posts
    449

    Re: How do I manipulate with the weeks?

    Thank you HanneSThEGreaT! Very helpful as always!!

    It really helped me and as you said, it's a very intelligent piece of code. Now with the help of this code I got the WEEK NUMBER of the date given by user. Now I want to perform some actions on particular days also given by user, for example... Monday & Thursday. How do I do that? Please check and help me with the code below. Thank you!

    ---------------------------------------------------------------------------------
    Dim weeknum As Integer = calendar.GetWeekOfYear(dr("sdate"), rule, firstDayOfWeek)
    .
    .
    .
    .
    .
    ------------------------------------------------------------------------------------

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How do I manipulate with the weeks?

    If I understand you correctly, you want to determine Monday and / or Thursday ....

    If that's the case, you can use the WeekDay function like this :
    Code:
    Dim MyWeekDay As Integer 'get the week day number
    
    MyWeekDay = Weekday(DateTime.Today) 'get the current weekday
    MessageBox.Show(MyWeekDay.ToString())
    Important thing to note here, is that if your firstday of the week is Sunday, it will return 2 for Monday and 5 for Thursday. If your first day of the week is set to Monday, then of course it will return 1 for Monday, and 4 for Thursday.

    Does this help ¿

  5. #5
    Join Date
    Oct 2006
    Posts
    449

    Re: How do I manipulate with the weeks?

    Thank you HanneSThEGreaT for your reply.

    But Hannes, I think I wasn't clear. Am sorry. Let me explain you clearly. I have a form in which the user gives the startdate (sdate) and I have a checkbox for all days (Sun, Mon, Tue, Wed, Thu, Fri, Sat) where the user will tick the checkbox on these. He may select only 'Mon' or select multiple days like 'Mon', 'Thu', 'Sat'). NOW I need to perform actions accordingly.

    NOW we got the week number of given date (sdate) and for this weeknumber and for the given (selected) days I need to perform an action. Hope am clear.

    So how do we have the statement for "For this week number and for the given days" perform the action.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How do I manipulate with the weeks?

    Lax, I've worked on something for you. The app I'm working on determines the week number, then allows the user to select any checkbox for the day of the week. Based on selection it adds the date of the day selected to a listbox. You should be able to use it, but I'll only be able to post the sample tomorrow.. If you don't mind

  7. #7
    Join Date
    Oct 2006
    Posts
    449

    Re: How do I manipulate with the weeks?

    Thank you Hannes! No problem I will definitely wait.
    If you too don't mind make sure that it is scalable to months too. (I mean the app determines the month number as week number and the user selects the months like days.

  8. #8
    Join Date
    Oct 2006
    Posts
    449

    Re: How do I manipulate with the weeks?

    Thank you Hannes! No problem I will definitely wait.

    If you too don't mind, can I ask you for little more... Can you make it scalable for months too? Like, the app determines the month number from the date given by the user like the week number and also the user selects the months like days. Just a thought, because it will really be useful!!

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How do I manipulate with the weeks?

    Hi Lax!

    I've made a sample for you. Basically how it works :
    You obtain the week number, then that date (which you'll enter into a textbox) will be stored in a variable. The reason for this is when the user selects a day checkbox, we already know that date of that monday or that particular thursday. Those dates then get added to the listbox. How I did this was, to use the AddDays function to add / subtract days from the original date.

    I'm sending the sample with, I hope you find it useful
    Last edited by HanneSThEGreaT; December 19th, 2008 at 03:14 AM.

  10. #10
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: How do I manipulate with the weeks?

    simply can do in selectchangedIndex event in sample above
    Code:
      Private Sub CheckBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.Click _
        , CheckBox2.Click, CheckBox3.Click, CheckBox4.Click, CheckBox5.Click, CheckBox6.Click, CheckBox7.Click
            If CType(sender, CheckBox).Checked Then
    
                Select Case CType(sender, CheckBox).Text
                    Case "Sunday"
                        i = 0
                    Case "Monday"
                        i = 1
                    Case "Tuesday"
                        i = 2
                    Case "Wednesday"
                        i = 3
                    Case "Thursday"
                        i = 4
                    Case "Friday"
                        i = 5
                    Case "Saturday"
                        i = 6
                End Select
    
                ListBox1.Items.Add(DateTime.Now.AddDays(i - DateTime.Today.DayOfWeek).ToShortDateString)
            End If
        End Sub

  11. #11
    Join Date
    Oct 2006
    Posts
    449

    Smile Re: How do I manipulate with the weeks?

    Thank you Hannes and Aniskhan! It was really helpful!!

  12. #12
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How do I manipulate with the weeks?

    You're welcome Lax!

    Keep up the good work!

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

Featured