Hi,
How Can we caliculate the number of the week of the current month.
thanks
Printable View
Hi,
How Can we caliculate the number of the week of the current month.
thanks
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.
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.
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
*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.
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
And did that work? I used this on my system and it worked perfectly:Looks pretty much the same as what you used except that Monday is the first day of the week on my system.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
ya,both are working same
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.
can we generalize the thing,like for any of the day,monday or sunday etc
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
using system settings is a good point
Thanks