Click to See Complete Forum and Search --> : [RESOLVED] How to find Week Of Year ?
Anwin
Apr 6th, 2006, 03:27 PM
Hi
Creating an ordinary windows application, I can use this code to find the week of year from a MonthCalendar control:
TextBox1.Text = DatePart(DateInterval.WeekOfYear, MonthCalendar1.SelectionStart)
However, using the same code in my Device app. for pocket pc, won't work
Hoping for an easy solution :)
Anwin
Anwin
Apr 16th, 2006, 12:42 PM
I couldn't find a answer to my problem, so I tried to make my own code for how to find a week of year from a given date.
I'm from Norway and we use this standards:
First day of week:
Monday (complies with ISO standard 8601, section 3.17)
First week of year:
Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)
After this standards this function will return the week (as integer) when calling the function with a given date (as date).
It might not be the easiest code, but as far as I can tell it's working.
VB.Net Code:
Function FindWeek(ByVal dtmToday As Date) As Integer
Dim intThisYear, intThisMonth, x As Integer
Dim dtmFirstDayOfWeek, FirstDayOfYear As Date
Dim MytimeSpan As TimeSpan
' Find first day of week
Do While dtmToday.DayOfWeek <> 1
intThisYear = dtmToday.Year
intThisMonth = dtmToday.Month
MytimeSpan = dtmToday.Subtract(CDate(1 & "." & intThisMonth & "." & intThisYear))
x = MytimeSpan.Days.ToString
If x = 0 Then
If intThisMonth = 1 Then
intThisMonth = 13
intThisYear = intThisYear - 1
End If
x = DateTime.DaysInMonth(intThisYear, intThisMonth - 1)
dtmToday = CDate(x & "." & intThisMonth - 1 & "." & intThisYear)
Else
dtmToday = CDate(x & "." & intThisMonth & "." & intThisYear)
End If
Loop
dtmFirstDayOfWeek = dtmToday
'Find week of year from first day of week
FirstDayOfYear = CDate("01.01." & dtmFirstDayOfWeek.ToString("yyyy"))
x = 0
If FirstDayOfYear.DayOfWeek = 5 Or FirstDayOfYear.DayOfWeek = 6 Or FirstDayOfYear.DayOfWeek = 0 Then
For x = 0 To dtmFirstDayOfWeek.DayOfYear + 1
Next x
x = (x / 7)
Else
For x = 1 To dtmFirstDayOfWeek.DayOfYear
Next x
x = (x / 7) + 1
If dtmFirstDayOfWeek.Month = 12 Then
If dtmFirstDayOfWeek.Day = 29 Then x = 1
If dtmFirstDayOfWeek.Day = 30 Then x = 1
If dtmFirstDayOfWeek.Day = 31 Then x = 1
End If
End If
Return x
End Function
Shaggy Hiker
Apr 23rd, 2006, 10:51 AM
I was lost in space when you originally posted, or I would have suggested something along those lines. The CF had to be stripped down to fit in the memory constraints of a PDA, and what was left out could be awkward. You just have to find a way around it, and it looks like you did.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.