|
-
Sep 18th, 2006, 09:18 AM
#1
Thread Starter
Hyperactive Member
[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
-
Sep 18th, 2006, 07:42 PM
#2
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.
-
Sep 19th, 2006, 12:03 AM
#3
Fanatic Member
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.
-
Sep 19th, 2006, 02:46 AM
#4
Thread Starter
Hyperactive Member
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
-
Sep 19th, 2006, 02:59 AM
#5
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.
-
Sep 19th, 2006, 03:08 AM
#6
Thread Starter
Hyperactive Member
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:
Dim First As DateTime = New DateTime(Now.Year, Now.Month, 1)
With System.Globalization.CultureInfo.CurrentCulture
Dim WeekOfNow As Integer = .Calendar.GetWeekOfYear(Now, Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)
Dim WeekOfFirst As Integer = .Calendar.GetWeekOfYear(First, Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)
Messagebox (1 + WeekOfNow - WeekOfFirst)
End With
-
Sep 19th, 2006, 03:24 AM
#7
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:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(Me.GetMonthWeekNumber(Me.DateTimePicker1.Value).ToString())
End Sub
Private Function GetMonthWeekNumber(ByVal [date] As Date) As Integer
Dim cal As Globalization.Calendar = Globalization.CultureInfo.CurrentCulture.Calendar
Dim monthStartWeek As Integer = cal.GetWeekOfYear(New Date([date].Year, _
[date].Month, _
1), _
Globalization.CalendarWeekRule.FirstDay, _
DayOfWeek.Monday)
Dim currentWeek As Integer = cal.GetWeekOfYear([date], _
Globalization.CalendarWeekRule.FirstDay, _
DayOfWeek.Monday)
Dim monthWeekNumber As Integer = currentWeek - monthStartWeek + 1
Return monthWeekNumber
End Function
End Class
Looks pretty much the same as what you used except that Monday is the first day of the week on my system.
-
Sep 19th, 2006, 03:54 AM
#8
Thread Starter
Hyperactive Member
Re: Number of the week of the current month -VBE
-
Sep 19th, 2006, 03:57 AM
#9
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.
-
Sep 19th, 2006, 04:01 AM
#10
Thread Starter
Hyperactive Member
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
-
Sep 19th, 2006, 04:14 AM
#11
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:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(Me.GetMonthWeekNumber(Me.DateTimePicker1.Value).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
End Class
-
Sep 19th, 2006, 04:19 AM
#12
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|