Click to See Complete Forum and Search --> : How do I manipulate with the weeks?
laxatcg1
January 29th, 2007, 09:07 AM
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.
HanneSThEGreaT
January 29th, 2007, 11:23 AM
Got this from a very clever dude once.. :thumb:
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
laxatcg1
January 29th, 2007, 11:24 PM
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)
.
.
.
.
.
------------------------------------------------------------------------------------
HanneSThEGreaT
January 29th, 2007, 11:35 PM
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 :
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 ¿
laxatcg1
January 30th, 2007, 12:23 AM
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.
HanneSThEGreaT
January 31st, 2007, 02:28 PM
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 :)
laxatcg1
February 1st, 2007, 03:48 AM
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.
laxatcg1
February 1st, 2007, 03:52 AM
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!!
HanneSThEGreaT
February 1st, 2007, 04:24 AM
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 :)
aniskhan
February 1st, 2007, 10:17 PM
simply can do in selectchangedIndex event in sample above 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
laxatcg1
February 6th, 2007, 10:04 PM
Thank you Hannes and Aniskhan! It was really helpful!!
HanneSThEGreaT
February 6th, 2007, 11:47 PM
You're welcome Lax! :)
Keep up the good work! :thumb:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.