Android Question GetWeekOfYear(DateTime.DateParse... Wrong

parijs

Active Member
Licensed User
Hi all

Why das
weeknr = GetWeekOfYear(DateTime.DateParse("05/05/2015"))

gives me week 18 if it falls in week 19
 

Attachments

Last edited:

parijs

Active Member
Licensed User
Sorry ;)

B4X:
Sub GetWeekOfYear(ticks As Long) As Int
   Return Floor(DateTime.GetDayOfYear(ticks) / 7) + 1
End Sub
 

parijs

Active Member
Licensed User
Hi Erel that code give me week 19 for 05/05

But also week 19 for 05/01 MM/dd and that is week 18
weeknr = GetWeekOfYear(DateTime.DateParse("05/01/2015")) :(
 
Last edited:

keirS

Well-Known Member
Licensed User
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
 

derez

Expert
Licensed User
B4X:
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
 

parijs

Active Member
Licensed User
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
 

keirS

Well-Known Member
Licensed User
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
 

parijs

Active Member
Licensed User
Hi KeirS

Great code but ;)

'Week 27'
Log(ISOWeekNumber("06/29/2015"))
Gives week 26

'Week 36'
Log(ISOWeekNumber("08/31/2015"))
Gives week 35

Whether this calendar is not correct
 

keirS

Well-Known Member
Licensed User
Change the Day1Week1( sub to this

B4X:
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
 

parijs

Active Member
Licensed User
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
 

keirS

Well-Known Member
Licensed User
ISO calender weeks start on Monday so my code works. I messed up on the translation to B4A first time round as it was written in FoxPro originally.
 

parijs

Active Member
Licensed User
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 :)
 

keirS

Well-Known Member
Licensed User
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 :)
It should be relatively simple to modify Erel's AnotherDatePickerClass to show the weeks how you want.
 
Top