|
-
Oct 4th, 2006, 07:02 AM
#1
Thread Starter
Lively Member
[2005] Getting current week number
Hi all
First of all I must apologize for brining this subject up yet again on these forums. I have searched for soluions and I also thought i found it.
However it seems to make some kind of miscalculation or something..
Here's one of the solutions I found in my search.
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
But when i compared the result with what i found on MS Outlook it didnt seem to match. Outlook, as does my program, use Monday as first day of week. But if you select the date 01.01.2006 in the DateTimePicker it will say that the weeknumber is 1. Outlook however claims it to be week #52.
(01.01.2006 is a sunday). This means that my program, using the code above, will claim that there are 53 weeks in this year..
Is there any other method I can use that will display the correct week number of a selected date?
-
Oct 4th, 2006, 07:19 AM
#2
Hyperactive Member
Re: [2005] Getting current week number
-
Oct 4th, 2006, 07:25 AM
#3
Thread Starter
Lively Member
Re: [2005] Getting current week number
I've tried with the code i found on that page (its the same as my original post), but it gave the same result. 01.01.2006 gives me week #1.
-
Oct 4th, 2006, 07:52 AM
#4
Hyperactive Member
Re: [2005] Getting current week number
hi,
Depending on how you count weeks(depending on where you live), the week number 1 can be:
the 1st of January
or
the first monday of January,
so if you want to have the second option, just check if 1st of january is monday if not substract 1 to the week number you have (be careful then all the days of january not included on the first week of year will be included on the last week of past year)
Hope this helps
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
|