I use this code in my apps for find weeknr. It works ok but between 00 and 1.00 the weeknumber is weeknumber -1 and after 1.00 its right again, can enyone help me to fiks that?
B4X:
Sub Process_Globals
Dim dt As AHDateTime
dt.Initialize
dt.Pattern = "w"
End Sub
Sub GetWeekNumber(ticks As Long) As Int
Dim currentThursday, firstThursday As Long
Dim savedDateFormat As String
currentThursday = GetThursdayOfWeekInTicks(ticks)
savedDateFormat = DateTime.DateFormat
DateTime.DateFormat = "MM/dd/yyyy"
firstThursday = GetThursdayOfWeekInTicks(DateTime.DateParse("01/04/" & (DateTime.GetYear(currentThursday))))
DateTime.DateFormat = savedDateFormat
Return Floor((currentThursday - firstThursday) / DateTime.TicksPerDay / 7 + 1)
End Sub
Sub GetThursdayOfWeekInTicks(ticks As Long) As Long
Dim diff As Int
Select DateTime.GetDayOfWeek(ticks)
' Sunday
Case 1
diff = -3
' Monday
Case 2
diff = 3
' Tuesdy
Case 3
diff = 2
' Wednesday
Case 4
diff = 1
' Thursday
Case 5
diff = 0
' Friday
Case 6
diff = -1
' Saturday
Case 7
diff = -2
End Select
Return DateTime.Add(ticks, 0, 0, diff)
End Sub