How to get Current Week Number of the Month

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 13
Reputation: Ravi Kant is an unknown quantity at this point 
Solved Threads: 1
Ravi Kant's Avatar
Ravi Kant Ravi Kant is offline Offline
Newbie Poster

How to get Current Week Number of the Month

 
0
  #1
Mar 4th, 2009
Hello !
I want to get the current week number of the month. I have used this code below : -

Private Sub Command1_Click()
Dim t_Day As Double
t_Day = Day(DTPicker1.Value)
Select Case t_Day
Case 1, 2, 3, 4, 5, 6, 7, 8
Text1.Text = "1st Week"
Case 9, 10, 11, 12, 13, 14, 15, 16
Text1.Text = "2nd Week"
Case 17, 18, 19, 20, 21, 22, 23, 24
Text1.Text = "3rd Week"
Case 25, 26, 27, 28, 29
Text1.Text = "4th Week"
Case 30, 31
Text1.Text = "5th Week"
End Select
End Sub

This does not return always true result. Any better code for this ????
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 902
Reputation: vb5prgrmr will become famous soon enough vb5prgrmr will become famous soon enough 
Solved Threads: 167
vb5prgrmr vb5prgrmr is offline Offline
Posting Shark

Re: How to get Current Week Number of the Month

 
0
  #2
Mar 4th, 2009
First off, your code does not take into account if the month starts on a saturday or any other day other than what is considered to be the first day of the week. Second, in a month with 31 days there could actually be 6 weeks in the month depending upon what day the month started and this would depend upon if you are using a fixed week or a dynamic week.

Fixed week means you consider the week to be set from sun to sat or mon to sun while a dynamic week means that the week starts on the first day of the month.

For a dynamic week it is quit simple
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. Dim Remainder As Integer, Week As Integer
  3.  
  4. Remainder = Day("5/15/2009") Mod 7
  5.  
  6. Week = Day("5/15/2009") \ 7
  7.  
  8. If Remainder > 0 Then Week = Week + 1
  9.  
  10. MsgBox Week

However, if you are using fixed weeks then...

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim Remainder As Integer, MyWeek As Integer
  2. Dim DayStart As Integer, MyDateString As String
  3.  
  4. MyDateString = "5/13/2009"
  5.  
  6. DayStart = Weekday(Month(MyDateString) & "/1/" & Year(MyDateString))
  7.  
  8. MyDateString = DateAdd("d", DayStart, MyDateString)
  9.  
  10. Remainder = Day(MyDateString) Mod 7
  11.  
  12. MyWeek = Day(MyDateString) \ 7
  13.  
  14. If Remainder > 0 Then MyWeek = MyWeek + 1
  15.  
  16. MsgBox MyWeek

Good Luck
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 13
Reputation: Ravi Kant is an unknown quantity at this point 
Solved Threads: 1
Ravi Kant's Avatar
Ravi Kant Ravi Kant is offline Offline
Newbie Poster

Re: How to get Current Week Number of the Month

 
0
  #3
Mar 4th, 2009
Thanks vb5prgrmr
But still my problem is not solved. I have DtPicker control in my form and i want the result according to it's value. I try you code giving date as dtpicker value but still problem is same.

Private Sub Command1_Click()

Dim Remainder As Integer, Week As Integer
Remainder = Day(DTPicker1.Value) Mod 7
Week = Day(DTPicker1.Value) \ 7
If Remainder > 0 Then Week = Week + 1
MsgBox Week
End Sub


Your second Code :

Private Sub Command2_Click()

Dim Remainder As Integer, MyWeek As Integer
Dim DayStart As Integer, MyDateString As String
MyDateString = DTPicker1.Value
DayStart = Weekday(Month(MyDateString) & "/1/" & Year(MyDateString))
MyDateString = DateAdd("d", DayStart, MyDateString)
Remainder = Day(MyDateString) Mod 7
MyWeek = Day(MyDateString) \ 7
If Remainder > 0 Then MyWeek = MyWeek + 1
MsgBox MyWeek

End Sub

But both these code does not give always correct result.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 902
Reputation: vb5prgrmr will become famous soon enough vb5prgrmr will become famous soon enough 
Solved Threads: 167
vb5prgrmr vb5prgrmr is offline Offline
Posting Shark

Re: How to get Current Week Number of the Month

 
0
  #4
Mar 4th, 2009
Originally Posted by Ravi Kant View Post
Thanks vb5prgrmr
But still my problem is not solved. I have DtPicker control in my form and i want the result according to it's value. I try you code giving date as dtpicker value but still problem is same.

Private Sub Command1_Click()

Dim Remainder As Integer, Week As Integer
Remainder = Day(DTPicker1.Value) Mod 7
Week = Day(DTPicker1.Value) \ 7
If Remainder > 0 Then Week = Week + 1
MsgBox Week
End Sub


Your second Code :

Private Sub Command2_Click()

Dim Remainder As Integer, MyWeek As Integer
Dim DayStart As Integer, MyDateString As String
MyDateString = DTPicker1.Value
DayStart = Weekday(Month(MyDateString) & "/1/" & Year(MyDateString))
MyDateString = DateAdd("d", DayStart, MyDateString)
Remainder = Day(MyDateString) Mod 7
MyWeek = Day(MyDateString) \ 7
If Remainder > 0 Then MyWeek = MyWeek + 1
MsgBox MyWeek

End Sub

But both these code does not give always correct result.
Yep, the second has an error because of a quick cut and paste. Just remove the "If Remainder" line
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: How to get Current Week Number of the Month

 
0
  #5
Mar 4th, 2009
You could use the MonthView Control.

The Week Property of that control returns that particular day's week number in relation to the current year.

For example

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim intWeek as integer
  2.  
  3. intWeek = MonthView1.week

intWeek should return the week number for the particular day selected in the MonthView Control.

You'll probably have to extra coding to figure out which week it is in relation to the month, though.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: How to get Current Week Number of the Month

 
0
  #6
Mar 4th, 2009
This should work. Might want to clean it up a bit and check the validity and logic of the Select Case Statement. It has some errors. I'll leave that to you to find. But something like the following:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2. Dim dFirstDayMonth As Date, intMonth As Integer, strMonthDayOne As String
  3. Dim intFirstSaturday As Integer, intFirstDay As Integer
  4. Dim strDayOne As String, intPickedDay As Integer
  5.  
  6. Private Sub cmdFindWeek_Click()
  7. Dim intPickedDay As Integer
  8. Dim intMonthWeek As Integer
  9. intPickedDay = DTPicker1.Day
  10. intMonth = DTPicker1.Month
  11. strMonthDayOne = CStr(intMonth) & "/1"
  12. dFirstDayMonth = CDate(strMonthDayOne)
  13. intFirstDay = Day(dFirstDayMonth)
  14. ' First Saturday should always be the following:
  15. intFirstSaturday = 8 - intFirstDay
  16. intMonthWeek = FindWeek(intPickedDay)
  17.  
  18. Text1 = "This is week " & CStr(intMonthWeek)
  19. End Sub
  20.  
  21. Private Function FindWeek(CurrentDay As Integer) As Integer
  22. Dim intFirstDayWeekTwo As Integer
  23. intFirstDayWeekTwo = intFirstSaturday + 1
  24. Select Case CurrentDay
  25.  
  26. Case 1 To intFirstSaturday
  27. FindWeek = 1
  28. Case intFirstDayWeekTwo To intFirstDayWeekTwo + 6
  29. FindWeek = 2
  30. Case intFirstDayWeekTwo + 7 To intFirstDayWeekTwo + 12
  31. FindWeek = 3
  32. Case intFirstDayWeekTwo + 14 To intFirstDayWeekTwo + 18
  33. FindWeek = 4
  34. Case intFirstDayWeekTwo + 21 To intFirstDayWeekTwo + 24
  35. FindWeek = 5
  36. Case intFirstDayWeekTwo + 28 To intFirstDayWeekTwo + 30
  37. FindWeek = 6
  38. End Select
  39.  
  40. End Function
Last edited by hkdani; Mar 4th, 2009 at 11:24 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 13
Reputation: Ravi Kant is an unknown quantity at this point 
Solved Threads: 1
Ravi Kant's Avatar
Ravi Kant Ravi Kant is offline Offline
Newbie Poster

Re: How to get Current Week Number of the Month

 
0
  #7
Mar 5th, 2009
Thanks hkdani !
But still it has problem . As for example for 5 january 2009 it is week 2 but it returns week 1 . Please check it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: How to get Current Week Number of the Month

 
0
  #8
Mar 5th, 2009
But still it has problem . As for example for 5 january 2009 it is week 2 but it returns week 1 . Please check it.
I'm leaving that up to you. I thought it was right, but testing it I did see a few bugs. I think it's a logic error on my part, but then again I also thought maybe the control itself wasn't working right. But I really doubt the control is giving the problem, it's probably me.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 13
Reputation: Ravi Kant is an unknown quantity at this point 
Solved Threads: 1
Ravi Kant's Avatar
Ravi Kant Ravi Kant is offline Offline
Newbie Poster

Re: How to get Current Week Number of the Month

 
0
  #9
Mar 5th, 2009
Please help ! I am new in VB.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: How to get Current Week Number of the Month

 
0
  #10
Mar 5th, 2009
Okay. The problem is in the Select Case CurrentDay , ..... End Select section.

As someone nicely pointed out in a previous post, you could have a possibility of 6 weeks in a month, if Day 1 fell on a Saturday (maybe even a Friday with 31 days?). That's why there are 6 cases: Case 1, Case 2, Case 3, ...., Case 6.

You simply have to determine that your tests in each of the 6 cases make sense.

Case 1 should always be from 1 to intFirstSaturday.
If you'll look at the code

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. ' First Saturday should always be the following:
  2. intFirstSaturday = 8 - intFirstDay

You should be able to find the actual day for the First Saturday of the month with that formula. The Day() fundtion returns which day of the week a certain day happens to fall on: Sunday, Monday, Tuesday, Wednesday, etc. So, if Day(#3/1/2009#) returns a value of 1, then 8 - 1 = 7: March 7, is Saturday, in other words.

That's the first test for Case 1

Case 2 should start with the Day after the first Saturday or intFirstSaturday + 1 or as in the above code: IntFirstDayWeekTwo.

We're testing from Sunday to Saturday or a period of 7 days. So
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Case (intFirstDayWeekTwo) to (intFirstDayWeekTwo + 6 )

Ah, I think I see the error. It begins in Case 3. I think I should be adding 7 instead of 6. So that should be + 13 instead of 12?, 20 instead of 18, etc.

That may give you enough to go on.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2003 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC