iOS Question Get numberofweek with first day = monday

schimanski

Well-Known Member
Licensed User
Longtime User
I tried both subs to get the number of the week of the year. But the result of the following dates ist always week 5. The reason seems to be the start of the week = sunday, and not monday.
How can I set the first day of the week in one of the following subs?

B4X:
Log(GetWeekNumber(1580030262356) & ", " & DateTime.Date(1580030262356) & " - " & DateTime.Time(1580030262356))
Log(GetWeekNumber(1580548662356) & ", " & DateTime.Date(1580548662356) & " - " & DateTime.Time(1580548662356))

5, 26.01.2020 - 10:17:42 = it's a sunday and in germany week 4
5, 01.02.2020 - 10:17:42


B4X:
Private Sub GetWeekNumber(DateTicks As Long) As Int
    Dim psCurrFmt As String = DateTime.DateFormat
    DateTime.DateFormat = "w"
    Dim piCurrentWeek As Int = DateTime.Date(DateTicks)
    DateTime.DateFormat = psCurrFmt
    Return piCurrentWeek
End Sub

'-or-

Sub GetWeekNumber(ticks As Long) As Int
    Dim offset As Int = DateTime.GetDayOfWeek(DateUtils.SetDate( _
     DateTime.GetYear(ticks), 1, 1)) - 1
    Return Floor((DateTime.GetDayOfYear(ticks) -1 + offset) / 7) + 1
End Sub

Thanks for help
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub GetWeekNumberStartingFromMonday (ticks As Long) As Int
    Dim firstdayOffset As Int = DateTime.GetDayOfWeek(DateUtils.SetDate( _
     DateTime.GetYear(ticks), 1, 1)) - 2
     If firstdayOffset < 0 Then firstdayOffset = firstdayOffset + 7
    Return Floor((DateTime.GetDayOfYear(ticks) - 1 + firstdayOffset) / 7) + 1
End Sub
 
Upvote 0
Top