Sub GetWeekOfYear(ticks As Long) As Int
Dim offset As Int = DateTime.GetDayOfWeek(DateUtils.SetDate( _
DateTime.GetYear(ticks), 1, 1))
Return Floor((DateTime.GetDayOfYear(ticks) + offset) / 7) + 1
End Sub
What do you mean by week number? There are several definitions of how to calculate week numbers. Starting from the simple at starting from the 1st of January to the ISO standard where week 1 can start in December of the previous year and contain 53 weeks in a year
Sub GetWeekOfYear(ticks As Long) As Int
Dim t As Long = DateTime.GetDayOfWeek(ticks)
Return DateTime.GetDayOfYear(ticks + (7-t) * DateTime.TicksPerDay) / 7 + 1
End Sub
hi
what I am trying to solve is that the app makes a monthly report off working hours.
The monthly report is divided into weeks.
So if I do not get the right weeknr its comes on the wrong place.
sorry I could not upload picas here so standing on the top
Here you go. These subs get the ISO Week number so for 2015 the first week actually starts on December 29th, 2014. I you are in Europe then this is probably what you want.
B4X:
Sub Activity_Create(FirstTime As Boolean)
'Week 1
Log(ISOWeekNumber("12/31/2014"))
'Week 18
Log(ISOWeekNumber("05/01/2015"))
'Week 19
Log(ISOWeekNumber("05/05/2015"))
'Week 53
Log(ISOWeekNumber("12/28/2015"))
End Sub
''Date in MM/DD/YYYY format
Sub ISOWeekNumber(Date As String)
Dim D1W1 As Long
Dim LW As Long
Dim Year As Int
Dim DiffDays As Float
Dim WeekNo As Long
'Get Date
Dim WeekNoDate = DateTime.DateParse(Date)
'Year
Year = DateTime.GetYear(DateTime.DateParse(Date))
'Get Starting Week for Year
D1W1 = Day1Week1(Year)
'Get Strrting Week For Next Year
LW = Day1Week1(Year + 1)
'If Date is in last week of previosu year then get the start of previous year
If WeekNoDate < D1W1 Then
D1W1 = Day1Week1(Year -1)
End If
'If Date is in the fisrts week of the next year then return 1
If WeekNoDate > LW Then
Return 1
Else
'Get the numbber of days from the start of the year
DiffDays = (WeekNoDate - D1W1) / DateTime.TicksPerDay
'Calculate the Week number
WeekNo = (DiffDays / 7) + 1
Return WeekNo
End If
End Sub
Sub Day1Week1(year As Int)
Dim StartDate As Long
Dim WkStart As Long
StartDate = DateTime.DatePArse("01/01/" & year)
WkStart = (((11 - DateTime.GetDayOfWeek(StartDate)) Mod 7) -3) * DateTime.TicksPerDay
Return StartDate - WkStart
End Sub
Sub Day1Week1(year As Int)
Dim StartDate As Long
Dim WkStart As Long
StartDate = DateTime.DatePArse("01/01/" & year)
WkStart = (((11 - DateTime.GetDayOfWeek(StartDate)) Mod 7) -3) * DateTime.TicksPerDay
Return StartDate - WkStart - DateTime.TicksPerDay
End Sub
unfortunately 05/31/2015 Gives week 22 but is week 23 "on the sunday cal"
But discovered another problem our calendar starts on Monday
the first week of May, have three days.
I use customCalendarExemple that starts on Sunday
the first week of May, two days there
first thanks for your help
Your code is excellent
My problem was that we use here the monday cal
And I use the Date Picker "sunday" calendar in my app
Example 3 may mdaycal is week 18, and 3may sundcal is week 19
When I do not use the weekend "i don't" than everything is okay
thumbs up for you
Sub GetWeekOfYear(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
first thanks for your help
Your code is excellent
My problem was that we use here the monday cal
And I use the Date Picker "sunday" calendar in my app
Example 3 may mdaycal is week 18, and 3may sundcal is week 19
When I do not use the weekend "i don't" than everything is okay
thumbs up for you