| | |
How to get Current Week Number of the Month
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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 ????
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 ????
•
•
Join Date: Mar 2009
Posts: 902
Reputation:
Solved Threads: 167
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
However, if you are using fixed weeks then...
Good Luck
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)
Dim Remainder As Integer, Week As Integer Remainder = Day("5/15/2009") Mod 7 Week = Day("5/15/2009") \ 7 If Remainder > 0 Then Week = Week + 1 MsgBox Week
However, if you are using fixed weeks then...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim Remainder As Integer, MyWeek As Integer Dim DayStart As Integer, MyDateString As String MyDateString = "5/13/2009" 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
Good Luck
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.
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.
•
•
Join Date: Mar 2009
Posts: 902
Reputation:
Solved Threads: 167
•
•
•
•
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.
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
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.
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)
Dim intWeek as integer 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.
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)
Option Explicit Dim dFirstDayMonth As Date, intMonth As Integer, strMonthDayOne As String Dim intFirstSaturday As Integer, intFirstDay As Integer Dim strDayOne As String, intPickedDay As Integer Private Sub cmdFindWeek_Click() Dim intPickedDay As Integer Dim intMonthWeek As Integer intPickedDay = DTPicker1.Day intMonth = DTPicker1.Month strMonthDayOne = CStr(intMonth) & "/1" dFirstDayMonth = CDate(strMonthDayOne) intFirstDay = Day(dFirstDayMonth) ' First Saturday should always be the following: intFirstSaturday = 8 - intFirstDay intMonthWeek = FindWeek(intPickedDay) Text1 = "This is week " & CStr(intMonthWeek) End Sub Private Function FindWeek(CurrentDay As Integer) As Integer Dim intFirstDayWeekTwo As Integer intFirstDayWeekTwo = intFirstSaturday + 1 Select Case CurrentDay Case 1 To intFirstSaturday FindWeek = 1 Case intFirstDayWeekTwo To intFirstDayWeekTwo + 6 FindWeek = 2 Case intFirstDayWeekTwo + 7 To intFirstDayWeekTwo + 12 FindWeek = 3 Case intFirstDayWeekTwo + 14 To intFirstDayWeekTwo + 18 FindWeek = 4 Case intFirstDayWeekTwo + 21 To intFirstDayWeekTwo + 24 FindWeek = 5 Case intFirstDayWeekTwo + 28 To intFirstDayWeekTwo + 30 FindWeek = 6 End Select End Function
Last edited by hkdani; Mar 4th, 2009 at 11:24 am.
•
•
•
•
But still it has problem . As for example for 5 january 2009 it is week 2 but it returns week 1 . Please check it.
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
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
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.
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)
' First Saturday should always be the following: 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)
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.
![]() |
Similar Threads
- Do I need to use strotime for making a birthday notification. (PHP)
- Customized calendar control challenge (ASP.NET)
- Advertise on my Gaming Sites and Anime Website (Ad Space for Sale)
- Really Cheap Ad Space Available! (Ad Space for Sale)
- The Value of Links On and Off Your Website (Search Engine Optimization)
- Transfer your Web site to WireNine.com Today! (Web Hosting Deals)
- check this code pleeeeeeeeeease (C++)
- Logic to Convert Days From 1800 to a Date (Month, Day, Year) (C++)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Reading CSV Files
- Next Thread: Target a web link, help me please.
Views: 2003 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Visual Basic 4 / 5 / 6
* 6 429 2007 access activex add age append application basic beginner birth bmp c++ calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver struct subroutine table tags time timer urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





