Label1.Text = "Current weeknumber: " & GetWeekNumber(DateTime.Now)
...
Sub GetWeekNumber(ticks As Long) As Int
Dim currentThursday, firstThursday As Long
currentThursday = GetThursdayOfWeekInTicks(ticks)
firstThursday = GetThursdayOfWeekInTicks(DateTime.DateParse("01/04/" & (DateTime.GetYear(currentThursday))))
Return Floor((currentThursday - firstThursday) / DateTime.TicksPerDay / 7 + 1)
End Sub
Sub GetThursdayOfWeekInTicks(ticks As Long) As Long
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
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
Why don't make it simple?
B4X:DateTime.DateFormat = "w" Log(DateTime.Date(DateTime.Now))
Yes yes thank you i works! Butt shoud not diff ben set as int or somting, it is red in my IDE but nor error.
hmmm... in my IDE there is nothing colored red...
Thats because the internal DateTime objects uses the Locale_US locale hardcoded. You can Use AHLocale DateTime object then it should be always correct.Important difference: The above DateFomat = "w" assumes that the week begins with sunday. Here in germany the first day of the week is monday. So I still have to put some code around it...
Dim dt As AHDateTime
dt.Initialize
dt.Pattern = "w"
weeknr = dt.format(DateTime.Now)
That reminds me to take a closer look to your shared libraries. :sign0098:You can Use AHLocale DateTime object then it should be always correct.