Android Question GetWeekNumberStartingFromMonday not working in 2021

schimanski

Well-Known Member
Licensed User
Longtime User
I'm using the following sub to calculate the weeknumber starting from monday. Until now, it worked fery fine. But now, in the new year, the sub seems no longer to work. The 03. January 2021 is the 53. week von the year 2020 because the week starts with 28.12.2020. So the 04.01.2021 must be the first week of the year, but it is the second in this sub. The weeknumber is always one week to much.

B4X:
DateTime.DateFormat = "dd.MM.yyyy"
Log(GetWeekNumberStartingFromMonday(DateTime.DateParse("03.01.2021")))

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

Thank you for help...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
January 1 always falls on week number 1.

PSYGRorwIf.png
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
???? Every calendar of 2021 says the same. I checked it also for USA: First kw is 04.01.2021...
F41CDC94-05D3-4A14-AB11-D887C7B123D9.jpeg
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
In germany it is other than in the USA for ex.


Kalenderwoche
Das Kalenderjahr umfasst mindestens 52 Wochen, die v. a. im Geschäftsleben als Kalenderwochen (KW) durchnummeriert werden, wobei es für die Wochen-Nummerierung verschiedene Definitionen gibt. Je nach angewandter Regel kann die erste Woche eines Jahres unterschiedlich festgelegt sein:


  • Im deutschsprachigen Raum, gemäß Normen der ISO, DIN, ÖNORM und SN: jene Woche, die den ersten Donnerstag des Jahres enthält (ISO 8601, früher DIN 1355-1). Da ISO 8601 den Montag als ersten Tag der Woche definiert, ist dies somit die erste Woche, von der mehr Tage (mindestens vier) auf das neue Jahr fallen als auf das alte Jahr. Äquivalent hierzu sind die folgenden Definitionen:
    • jene Woche, die den 4. Januar enthält
    • jene Woche, die den 1. Januar enthält, falls dieser ein Montag, Dienstag, Mittwoch oder Donnerstag ist, sonst die darauf folgende Woche
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
For what I see, in the USA calendar the 04.01.2021 is also the KW 1.
But it uses nothing. The sub above seems not to work all the time for ISO germany...If there is no other solution, i think, i have to define all the exceptions.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Not tested enough:
B4X:
Sub AppStart (Args() As String)
    DateTime.DateFormat = "dd.MM.yyyy"
    Log(GetWeekNumberStartingFromMonday(DateTime.DateParse("01.01.2021")))
    Log(GetWeekNumberStartingFromMonday(DateTime.DateParse("02.01.2021")))
    Log(GetWeekNumberStartingFromMonday(DateTime.DateParse("03.01.2021")))
    Log(GetWeekNumberStartingFromMonday(DateTime.DateParse("04.01.2021")))
    Log(GetWeekNumberStartingFromMonday(DateTime.DateParse("05.01.2021")))
End Sub


Sub GetWeekNumberStartingFromMonday (ticks As Long) As Int
    Dim WeekDayOfFirstDayOfYear As Int = DateTime.GetDayOfWeek(DateUtils.SetDate(DateTime.GetYear(ticks), 1, 1)) - 1
    Dim FirstMondayInYear As Int = (7 + WeekDayOfFirstDayOfYear - 1) Mod 7
    Dim result As Int
    If WeekDayOfFirstDayOfYear <> 1 Then result = result - 1
    result = result + Floor((DateTime.GetDayOfYear(ticks) - 1 + FirstMondayInYear) / 7) + 1
    Return result
End Sub
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I haven't compared to Erels code, but I just realized that I saw something long ago and digged it up again. Here's a javascript implementation of that, perhaps it can help with some logic or whatnot.

https://github.com/jquery/jquery-ui...29fc42574cf85c/ui/widgets/datepicker.js#L1105

Calculate week from date using javascript:
/* Set as calculateWeek to determine the week of the year based on the ISO 8601 definition.
 * @param  date  Date - the date to get the week for
 * @return  number - the number of the week within the year that contains this date
 */
iso8601Week: function( date ) {
    var time,
        checkDate = new Date( date.getTime() );

    // Find Thursday of this week starting on Monday
    checkDate.setDate( checkDate.getDate() + 4 - ( checkDate.getDay() || 7 ) );

    time = checkDate.getTime();
    checkDate.setMonth( 0 ); // Compare with Jan 1
    checkDate.setDate( 1 );
    return Math.floor( Math.round( ( time - checkDate ) / 86400000 ) / 7 ) + 1;
}
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Thank you for all efforts!
The following code does not work. With the date from today, the KW is alredy one week to low. The sub makes the right cut to the 04.01.2021, but the 01. to 03.01. must be KW 53, not 0. I tried to increase the week on different parts, but with no useful result.Seems to be a bigger challenge?

B4X:
Sub GetWeekNumberStartingFromMonday (ticks As Long) As Int
    Dim WeekDayOfFirstDayOfYear As Int = DateTime.GetDayOfWeek(DateUtils.SetDate(DateTime.GetYear(ticks), 1, 1)) - 1
    Dim FirstMondayInYear As Int = (7 + WeekDayOfFirstDayOfYear - 1) Mod 7
    Dim result As Int
    If WeekDayOfFirstDayOfYear <> 1 Then result = result - 1
    result = result + Floor((DateTime.GetDayOfYear(ticks) - 1 + FirstMondayInYear) / 7) + 1
    Return result
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
As @Erel keeps telling us since years, working with dates is more complex than it appears at first... :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code does not work. With the date from today, the KW is alredy one week to low. The sub makes the right cut to the 04.01.2021, but the 01. to 03.01. must be KW 53, not 0
This works as expected. You should treat 0 as week 53 of the previous year, if it returned 53 then you wouldn't know whether it is 53 of this year or the previous one.
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Ok, thank you, that's obvios.
But as example, the 13.11.2020 gets a the result of 45, but it has to be 46. In the following year, the 13.11.2021 gets the 45 week, that is correct. The sub works until 2024. In 2025 and 2026, the weeknumber is again one week to low for each date. In 2027, the sub works again correct.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
13.11.2020 is week #45.

B4X:
Sub AppStart (Args() As String)
    DateTime.DateFormat = "dd.MM.yyyy"
    Dim start As Long = DateUtils.SetDate(2020, 1, 1)
    For i = 0 To 350
        Dim p As Period
        p.Days = i
        Dim t As Long = DateUtils.AddPeriod(start, p)
        If DateTime.GetDayOfWeek(t) = 2 Then
            Log("********************")
        End If
        Log($"$Date{t}: Week #${GetWeekNumberStartingFromMonday(t)}, Day: ${DateUtils.GetDayOfWeekName(t)}"$)
    Next
End Sub

Sub GetWeekNumberStartingFromMonday (ticks As Long) As Int
    Dim WeekDayOfFirstDayOfYear As Int = DateTime.GetDayOfWeek(DateUtils.SetDate(DateTime.GetYear(ticks), 1, 1)) - 1
    Dim FirstMondayInYear As Int = (7 + WeekDayOfFirstDayOfYear - 1) Mod 7
    Dim result As Int
    If WeekDayOfFirstDayOfYear <> 1 Then result = result - 1
    result = result + Floor((DateTime.GetDayOfYear(ticks) - 1 + FirstMondayInYear) / 7) + 1
    Return result
End Sub

01.01.2020: Week #0, Day: Wednesday
02.01.2020: Week #0, Day: Thursday
03.01.2020: Week #0, Day: Friday
04.01.2020: Week #0, Day: Saturday
05.01.2020: Week #0, Day: Sunday
********************
06.01.2020: Week #1, Day: Monday
07.01.2020: Week #1, Day: Tuesday
08.01.2020: Week #1, Day: Wednesday
09.01.2020: Week #1, Day: Thursday
10.01.2020: Week #1, Day: Friday
11.01.2020: Week #1, Day: Saturday
12.01.2020: Week #1, Day: Sunday
********************
13.01.2020: Week #2, Day: Monday
14.01.2020: Week #2, Day: Tuesday
15.01.2020: Week #2, Day: Wednesday
16.01.2020: Week #2, Day: Thursday
17.01.2020: Week #2, Day: Friday
18.01.2020: Week #2, Day: Saturday
19.01.2020: Week #2, Day: Sunday
********************
20.01.2020: Week #3, Day: Monday
21.01.2020: Week #3, Day: Tuesday
22.01.2020: Week #3, Day: Wednesday
23.01.2020: Week #3, Day: Thursday
24.01.2020: Week #3, Day: Friday
25.01.2020: Week #3, Day: Saturday
26.01.2020: Week #3, Day: Sunday
********************
27.01.2020: Week #4, Day: Monday
28.01.2020: Week #4, Day: Tuesday
29.01.2020: Week #4, Day: Wednesday
30.01.2020: Week #4, Day: Thursday
31.01.2020: Week #4, Day: Friday
01.02.2020: Week #4, Day: Saturday
02.02.2020: Week #4, Day: Sunday
********************
03.02.2020: Week #5, Day: Monday
04.02.2020: Week #5, Day: Tuesday
05.02.2020: Week #5, Day: Wednesday
06.02.2020: Week #5, Day: Thursday
07.02.2020: Week #5, Day: Friday
08.02.2020: Week #5, Day: Saturday
09.02.2020: Week #5, Day: Sunday
********************
10.02.2020: Week #6, Day: Monday
11.02.2020: Week #6, Day: Tuesday
12.02.2020: Week #6, Day: Wednesday
13.02.2020: Week #6, Day: Thursday
14.02.2020: Week #6, Day: Friday
15.02.2020: Week #6, Day: Saturday
16.02.2020: Week #6, Day: Sunday
********************
17.02.2020: Week #7, Day: Monday
18.02.2020: Week #7, Day: Tuesday
19.02.2020: Week #7, Day: Wednesday
20.02.2020: Week #7, Day: Thursday
21.02.2020: Week #7, Day: Friday
22.02.2020: Week #7, Day: Saturday
23.02.2020: Week #7, Day: Sunday
********************
24.02.2020: Week #8, Day: Monday
25.02.2020: Week #8, Day: Tuesday
26.02.2020: Week #8, Day: Wednesday
27.02.2020: Week #8, Day: Thursday
28.02.2020: Week #8, Day: Friday
29.02.2020: Week #8, Day: Saturday
01.03.2020: Week #8, Day: Sunday
********************
02.03.2020: Week #9, Day: Monday
03.03.2020: Week #9, Day: Tuesday
04.03.2020: Week #9, Day: Wednesday
05.03.2020: Week #9, Day: Thursday
06.03.2020: Week #9, Day: Friday
07.03.2020: Week #9, Day: Saturday
08.03.2020: Week #9, Day: Sunday
********************
09.03.2020: Week #10, Day: Monday
10.03.2020: Week #10, Day: Tuesday
11.03.2020: Week #10, Day: Wednesday
12.03.2020: Week #10, Day: Thursday
13.03.2020: Week #10, Day: Friday
14.03.2020: Week #10, Day: Saturday
15.03.2020: Week #10, Day: Sunday
********************
16.03.2020: Week #11, Day: Monday
17.03.2020: Week #11, Day: Tuesday
18.03.2020: Week #11, Day: Wednesday
19.03.2020: Week #11, Day: Thursday
20.03.2020: Week #11, Day: Friday
21.03.2020: Week #11, Day: Saturday
22.03.2020: Week #11, Day: Sunday
********************
23.03.2020: Week #12, Day: Monday
24.03.2020: Week #12, Day: Tuesday
25.03.2020: Week #12, Day: Wednesday
26.03.2020: Week #12, Day: Thursday
27.03.2020: Week #12, Day: Friday
28.03.2020: Week #12, Day: Saturday
29.03.2020: Week #12, Day: Sunday
********************
30.03.2020: Week #13, Day: Monday
31.03.2020: Week #13, Day: Tuesday
01.04.2020: Week #13, Day: Wednesday
02.04.2020: Week #13, Day: Thursday
03.04.2020: Week #13, Day: Friday
04.04.2020: Week #13, Day: Saturday
05.04.2020: Week #13, Day: Sunday
********************
06.04.2020: Week #14, Day: Monday
07.04.2020: Week #14, Day: Tuesday
08.04.2020: Week #14, Day: Wednesday
09.04.2020: Week #14, Day: Thursday
10.04.2020: Week #14, Day: Friday
11.04.2020: Week #14, Day: Saturday
12.04.2020: Week #14, Day: Sunday
********************
13.04.2020: Week #15, Day: Monday
14.04.2020: Week #15, Day: Tuesday
15.04.2020: Week #15, Day: Wednesday
16.04.2020: Week #15, Day: Thursday
17.04.2020: Week #15, Day: Friday
18.04.2020: Week #15, Day: Saturday
19.04.2020: Week #15, Day: Sunday
********************
20.04.2020: Week #16, Day: Monday
21.04.2020: Week #16, Day: Tuesday
22.04.2020: Week #16, Day: Wednesday
23.04.2020: Week #16, Day: Thursday
24.04.2020: Week #16, Day: Friday
25.04.2020: Week #16, Day: Saturday
26.04.2020: Week #16, Day: Sunday
********************
27.04.2020: Week #17, Day: Monday
28.04.2020: Week #17, Day: Tuesday
29.04.2020: Week #17, Day: Wednesday
30.04.2020: Week #17, Day: Thursday
01.05.2020: Week #17, Day: Friday
02.05.2020: Week #17, Day: Saturday
03.05.2020: Week #17, Day: Sunday
********************
04.05.2020: Week #18, Day: Monday
05.05.2020: Week #18, Day: Tuesday
06.05.2020: Week #18, Day: Wednesday
07.05.2020: Week #18, Day: Thursday
08.05.2020: Week #18, Day: Friday
09.05.2020: Week #18, Day: Saturday
10.05.2020: Week #18, Day: Sunday
********************
11.05.2020: Week #19, Day: Monday
12.05.2020: Week #19, Day: Tuesday
13.05.2020: Week #19, Day: Wednesday
14.05.2020: Week #19, Day: Thursday
15.05.2020: Week #19, Day: Friday
16.05.2020: Week #19, Day: Saturday
17.05.2020: Week #19, Day: Sunday
********************
18.05.2020: Week #20, Day: Monday
19.05.2020: Week #20, Day: Tuesday
20.05.2020: Week #20, Day: Wednesday
21.05.2020: Week #20, Day: Thursday
22.05.2020: Week #20, Day: Friday
23.05.2020: Week #20, Day: Saturday
24.05.2020: Week #20, Day: Sunday
********************
25.05.2020: Week #21, Day: Monday
26.05.2020: Week #21, Day: Tuesday
27.05.2020: Week #21, Day: Wednesday
28.05.2020: Week #21, Day: Thursday
29.05.2020: Week #21, Day: Friday
30.05.2020: Week #21, Day: Saturday
31.05.2020: Week #21, Day: Sunday
********************
01.06.2020: Week #22, Day: Monday
02.06.2020: Week #22, Day: Tuesday
03.06.2020: Week #22, Day: Wednesday
04.06.2020: Week #22, Day: Thursday
05.06.2020: Week #22, Day: Friday
06.06.2020: Week #22, Day: Saturday
07.06.2020: Week #22, Day: Sunday
********************
08.06.2020: Week #23, Day: Monday
09.06.2020: Week #23, Day: Tuesday
10.06.2020: Week #23, Day: Wednesday
11.06.2020: Week #23, Day: Thursday
12.06.2020: Week #23, Day: Friday
13.06.2020: Week #23, Day: Saturday
14.06.2020: Week #23, Day: Sunday
********************
15.06.2020: Week #24, Day: Monday
16.06.2020: Week #24, Day: Tuesday
17.06.2020: Week #24, Day: Wednesday
18.06.2020: Week #24, Day: Thursday
19.06.2020: Week #24, Day: Friday
20.06.2020: Week #24, Day: Saturday
21.06.2020: Week #24, Day: Sunday
********************
22.06.2020: Week #25, Day: Monday
23.06.2020: Week #25, Day: Tuesday
24.06.2020: Week #25, Day: Wednesday
25.06.2020: Week #25, Day: Thursday
26.06.2020: Week #25, Day: Friday
27.06.2020: Week #25, Day: Saturday
28.06.2020: Week #25, Day: Sunday
********************
29.06.2020: Week #26, Day: Monday
30.06.2020: Week #26, Day: Tuesday
01.07.2020: Week #26, Day: Wednesday
02.07.2020: Week #26, Day: Thursday
03.07.2020: Week #26, Day: Friday
04.07.2020: Week #26, Day: Saturday
05.07.2020: Week #26, Day: Sunday
********************
06.07.2020: Week #27, Day: Monday
07.07.2020: Week #27, Day: Tuesday
08.07.2020: Week #27, Day: Wednesday
09.07.2020: Week #27, Day: Thursday
10.07.2020: Week #27, Day: Friday
11.07.2020: Week #27, Day: Saturday
12.07.2020: Week #27, Day: Sunday
********************
13.07.2020: Week #28, Day: Monday
14.07.2020: Week #28, Day: Tuesday
15.07.2020: Week #28, Day: Wednesday
16.07.2020: Week #28, Day: Thursday
17.07.2020: Week #28, Day: Friday
18.07.2020: Week #28, Day: Saturday
19.07.2020: Week #28, Day: Sunday
********************
20.07.2020: Week #29, Day: Monday
21.07.2020: Week #29, Day: Tuesday
22.07.2020: Week #29, Day: Wednesday
23.07.2020: Week #29, Day: Thursday
24.07.2020: Week #29, Day: Friday
25.07.2020: Week #29, Day: Saturday
26.07.2020: Week #29, Day: Sunday
********************
27.07.2020: Week #30, Day: Monday
28.07.2020: Week #30, Day: Tuesday
29.07.2020: Week #30, Day: Wednesday
30.07.2020: Week #30, Day: Thursday
31.07.2020: Week #30, Day: Friday
01.08.2020: Week #30, Day: Saturday
02.08.2020: Week #30, Day: Sunday
********************
03.08.2020: Week #31, Day: Monday
04.08.2020: Week #31, Day: Tuesday
05.08.2020: Week #31, Day: Wednesday
06.08.2020: Week #31, Day: Thursday
07.08.2020: Week #31, Day: Friday
08.08.2020: Week #31, Day: Saturday
09.08.2020: Week #31, Day: Sunday
********************
10.08.2020: Week #32, Day: Monday
11.08.2020: Week #32, Day: Tuesday
12.08.2020: Week #32, Day: Wednesday
13.08.2020: Week #32, Day: Thursday
14.08.2020: Week #32, Day: Friday
15.08.2020: Week #32, Day: Saturday
16.08.2020: Week #32, Day: Sunday
********************
17.08.2020: Week #33, Day: Monday
18.08.2020: Week #33, Day: Tuesday
19.08.2020: Week #33, Day: Wednesday
20.08.2020: Week #33, Day: Thursday
21.08.2020: Week #33, Day: Friday
22.08.2020: Week #33, Day: Saturday
23.08.2020: Week #33, Day: Sunday
********************
24.08.2020: Week #34, Day: Monday
25.08.2020: Week #34, Day: Tuesday
26.08.2020: Week #34, Day: Wednesday
27.08.2020: Week #34, Day: Thursday
28.08.2020: Week #34, Day: Friday
29.08.2020: Week #34, Day: Saturday
30.08.2020: Week #34, Day: Sunday
********************
31.08.2020: Week #35, Day: Monday
01.09.2020: Week #35, Day: Tuesday
02.09.2020: Week #35, Day: Wednesday
03.09.2020: Week #35, Day: Thursday
04.09.2020: Week #35, Day: Friday
05.09.2020: Week #35, Day: Saturday
06.09.2020: Week #35, Day: Sunday
********************
07.09.2020: Week #36, Day: Monday
08.09.2020: Week #36, Day: Tuesday
09.09.2020: Week #36, Day: Wednesday
10.09.2020: Week #36, Day: Thursday
11.09.2020: Week #36, Day: Friday
12.09.2020: Week #36, Day: Saturday
13.09.2020: Week #36, Day: Sunday
********************
14.09.2020: Week #37, Day: Monday
15.09.2020: Week #37, Day: Tuesday
16.09.2020: Week #37, Day: Wednesday
17.09.2020: Week #37, Day: Thursday
18.09.2020: Week #37, Day: Friday
19.09.2020: Week #37, Day: Saturday
20.09.2020: Week #37, Day: Sunday
********************
21.09.2020: Week #38, Day: Monday
22.09.2020: Week #38, Day: Tuesday
23.09.2020: Week #38, Day: Wednesday
24.09.2020: Week #38, Day: Thursday
25.09.2020: Week #38, Day: Friday
26.09.2020: Week #38, Day: Saturday
27.09.2020: Week #38, Day: Sunday
********************
28.09.2020: Week #39, Day: Monday
29.09.2020: Week #39, Day: Tuesday
30.09.2020: Week #39, Day: Wednesday
01.10.2020: Week #39, Day: Thursday
02.10.2020: Week #39, Day: Friday
03.10.2020: Week #39, Day: Saturday
04.10.2020: Week #39, Day: Sunday
********************
05.10.2020: Week #40, Day: Monday
06.10.2020: Week #40, Day: Tuesday
07.10.2020: Week #40, Day: Wednesday
08.10.2020: Week #40, Day: Thursday
09.10.2020: Week #40, Day: Friday
10.10.2020: Week #40, Day: Saturday
11.10.2020: Week #40, Day: Sunday
********************
12.10.2020: Week #41, Day: Monday
13.10.2020: Week #41, Day: Tuesday
14.10.2020: Week #41, Day: Wednesday
15.10.2020: Week #41, Day: Thursday
16.10.2020: Week #41, Day: Friday
17.10.2020: Week #41, Day: Saturday
18.10.2020: Week #41, Day: Sunday
********************
19.10.2020: Week #42, Day: Monday
20.10.2020: Week #42, Day: Tuesday
21.10.2020: Week #42, Day: Wednesday
22.10.2020: Week #42, Day: Thursday
23.10.2020: Week #42, Day: Friday
24.10.2020: Week #42, Day: Saturday
25.10.2020: Week #42, Day: Sunday
********************
26.10.2020: Week #43, Day: Monday
27.10.2020: Week #43, Day: Tuesday
28.10.2020: Week #43, Day: Wednesday
29.10.2020: Week #43, Day: Thursday
30.10.2020: Week #43, Day: Friday
31.10.2020: Week #43, Day: Saturday
01.11.2020: Week #43, Day: Sunday
********************
02.11.2020: Week #44, Day: Monday
03.11.2020: Week #44, Day: Tuesday
04.11.2020: Week #44, Day: Wednesday
05.11.2020: Week #44, Day: Thursday
06.11.2020: Week #44, Day: Friday
07.11.2020: Week #44, Day: Saturday
08.11.2020: Week #44, Day: Sunday
********************
09.11.2020: Week #45, Day: Monday
10.11.2020: Week #45, Day: Tuesday
11.11.2020: Week #45, Day: Wednesday
12.11.2020: Week #45, Day: Thursday
13.11.2020: Week #45, Day: Friday
14.11.2020: Week #45, Day: Saturday
15.11.2020: Week #45, Day: Sunday
********************
16.11.2020: Week #46, Day: Monday
17.11.2020: Week #46, Day: Tuesday
18.11.2020: Week #46, Day: Wednesday
19.11.2020: Week #46, Day: Thursday
20.11.2020: Week #46, Day: Friday
21.11.2020: Week #46, Day: Saturday
22.11.2020: Week #46, Day: Sunday
********************
23.11.2020: Week #47, Day: Monday
24.11.2020: Week #47, Day: Tuesday
25.11.2020: Week #47, Day: Wednesday
26.11.2020: Week #47, Day: Thursday
27.11.2020: Week #47, Day: Friday
28.11.2020: Week #47, Day: Saturday
29.11.2020: Week #47, Day: Sunday
********************
30.11.2020: Week #48, Day: Monday
01.12.2020: Week #48, Day: Tuesday
02.12.2020: Week #48, Day: Wednesday
03.12.2020: Week #48, Day: Thursday
04.12.2020: Week #48, Day: Friday
05.12.2020: Week #48, Day: Saturday
06.12.2020: Week #48, Day: Sunday
********************
07.12.2020: Week #49, Day: Monday
08.12.2020: Week #49, Day: Tuesday
09.12.2020: Week #49, Day: Wednesday
10.12.2020: Week #49, Day: Thursday
11.12.2020: Week #49, Day: Friday
12.12.2020: Week #49, Day: Saturday
13.12.2020: Week #49, Day: Sunday
********************
14.12.2020: Week #50, Day: Monday
15.12.2020: Week #50, Day: Tuesday
16.12.2020: Week #50, Day: Wednesday
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
The sub works until 2024. In 2025 and 2026, the weeknumber is again one week to low for each date

why bother with something that will crash only in 5-6 years? until then b4i will be 100% free, trump will be again president and we will make vacations on the moon.
(i am sure that until then you will need to change your code several times because of google policy so don't worry, be happy :) )
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Ok, I think, we don't speek from the same ISO. In germany, this is

06.01.2020: Week #1, Day: Monday
07.01.2020: Week #1, Day: Tuesday
08.01.2020: Week #1, Day: Wednesday
09.01.2020: Week #1, Day: Thursday
10.01.2020: Week #1, Day: Friday
11.01.2020: Week #1, Day: Saturday
12.01.2020: Week #1, Day: Sunday

in all calendars the second week, not the first. In germany, the crucial criterion for the week number is, that if more days of the beginning of january are in the first week than days of december, the week is number one, otherwise it is the last week of the year before. So in germany, the first week of a year could not start with the 06.01., because there are 5 days in january before the 06.01. It must be the second week.
 
Last edited:
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
The attached example is not very elegant and must be refined, but it works in all cases. The results are congruent with the german calendars of the next years:

B4X:
Sub GetWeeekNumberISO8601(Datum As Long) As Int
   
    Jahr=DateTime.GetYear(Datum)
   
    Dim ErsterTagdesJahres As String = "01.01." & Jahr
    Dim fdy As Int = GetWeekDay(DateTime.DateParse(ErsterTagdesJahres))                        'Wochetag für den 01.01. ermitteln
   
    Dim TagdesJahres As Int = DateTime.GetDayOfYear(Datum)                                    '1. // Anzahl der Tage im Jahr ermitteln                  
   
    Dim p As Period
    p.Days = -((DateTime.GetDayOfWeek(Datum)+5) Mod 7) 'change to 5 to start the week from Monday
    Dim WochenTag As Int = (-p.Days)+1                                                        '2. // Wochentag ermitteln
'  
    KW=((7-fdy)+(TagdesJahres-WochenTag+7))/7                                                '3. // die Wochennummer ohne Sonderfall berechnen
   
    Select KW      
        Case 0                                                                                '4. // wenn die Wochennummer 0 ergibt, dann liegt der Tag am Anfang des Jahres (1. Sonderfall)
            WochenTag = GetWeekDay(DateTime.DateParse("31.12." & (Jahr-1)))                    '5. // den letzten Wochentag aus dem Vorjahr ermitteln
            If DateTime.GetDayOfYear(DateTime.DateParse("31.12." & (Jahr-1)))=366 Then         '6. // ermitteln, ob es sich beim Vorjahr um ein Schaltjahr handelt
                Dim LeapYear As Boolean=True
            Else
                Dim LeapYear As Boolean=False
            End If                                                                             '7. // und nach dem Switch weitermachen...
        Case 52,53      
            'Hier muss geklärt werden, ob der 31.12. überhaupt in dieser Woche liegt:          '8. // wenn die Wochennummer 52 oder 53 ergibt, dann liegt der Tag am Ende des Jahres (2. Sonderfall)
            If IncludesEndofYear(Datum)= True Then
                WochenTag = GetWeekDay(DateTime.DateParse("31.12." & (Jahr)))                  '9. // den letzten Wochentag aus diesem Jahr ermitteln
                If DateTime.GetDayOfYear(DateTime.DateParse("31.12." & (Jahr)))=366 Then      '10.// ermitteln, ob es sich bei diesem Jahr um ein Schaltjahr handelt
                    Dim LeapYear As Boolean=True
                Else
                    Dim LeapYear As Boolean=False
                End If  
            Else
                Return KW
            End If                                                                         '11.// und nach dem Switch weitermachen...
        Case Else                                                                           '12.// in den anderen Faellen kann die Funktion hier verlassen und die Wochennummer zurueckgegeben werden
            Return KW
    End Select
   
    If WochenTag < 4 Then                                                                 '14.// wenn der 31.12. vor dem Donnerstag liegt, dann...
        KW = 1                                                                              '15.// ist das die erste Woche des Jahres
    Else                                                                                   '16.// anderenfalls muss ermittelt werden, ob es eine 53. Woche gibt (3. Sonderfall)
        '// wenn der letzte WochenTag auf einen Donnerstag oder,        
        '// in einem Schaltjahr, auf einen Donnerstag oder Freitag fällt,
        '// dann ist das die 53. Woche, ansonsten die 52. Woche.
        If WochenTag=4 Or LeapYear=True And WochenTag=5 Then
            KW=53
        Else
            KW=52
        End If
    End If
    Return KW
   
End Sub

Private Sub IncludesEndofYear(Datum As Long) As Boolean
    Dim p As Period
    p.Days = -((DateTime.GetDayOfWeek(Datum)+5) Mod 7) 'change to 5 to start the week from Monday
    Dim FirstDay As Long = Datum + DateTime.TicksPerDay*p.Days
    Dim LastDay As Long = FirstDay + WochenPeriode
    'Log("Erster Tag der Woche: " & DateTime.Date(FirstDay))
    'Log("Letzter Tag der Woche: " & DateTime.Date(LastDay))
    If LastDay>=DateTime.DateParse("31.12." & (Jahr)) Then
        Return True
    Else
        Return False
    End If
End Sub

Private Sub GetWeekDay(DatumS As Long) As Int
    DateTime.DateFormat = "dd.MM.yyyy"
    Dim p As Period
    p.Days = -((DateTime.GetDayOfWeek(DatumS)+5) Mod 7) 'change to 5 to start the week from Monday
    Return ((-p.Days)+1)
End Sub

B4X:
********************
30.12.2019: Week #1, Day: Montag
31.12.2019: Week #1, Day: Dienstag
01.01.2020: Week #1, Day: Mittwoch
02.01.2020: Week #1, Day: Donnerstag
03.01.2020: Week #1, Day: Freitag
04.01.2020: Week #1, Day: Samstag
05.01.2020: Week #1, Day: Sonntag
********************
06.01.2020: Week #2, Day: Montag
07.01.2020: Week #2, Day: Dienstag
08.01.2020: Week #2, Day: Mittwoch
09.01.2020: Week #2, Day: Donnerstag
10.01.2020: Week #2, Day: Freitag
11.01.2020: Week #2, Day: Samstag
12.01.2020: Week #2, Day: Sonntag
********************
13.01.2020: Week #3, Day: Montag
14.01.2020: Week #3, Day: Dienstag
15.01.2020: Week #3, Day: Mittwoch
16.01.2020: Week #3, Day: Donnerstag
17.01.2020: Week #3, Day: Freitag
18.01.2020: Week #3, Day: Samstag
19.01.2020: Week #3, Day: Sonntag
********************
20.01.2020: Week #4, Day: Montag
21.01.2020: Week #4, Day: Dienstag
22.01.2020: Week #4, Day: Mittwoch
 

Attachments

  • weeknumberISO.zip
    22.3 KB · Views: 232
Last edited:
Upvote 0

astronald

Active Member
Licensed User
Longtime User
i try this code for iso week
isoweek:
Sub isoGetWeekOfYear(ticks As Long) As Int
    Dim offset As Int =  0 
    Dim FirstDay As Long  = DateUtils.SetDate(DateTime.GetYear(ticks), 1, 1) 
    If DateTime.GetDayOfWeek(FirstDay) >5 Then ' is Thursday in Week?
        Do While(DateTime.GetDayOfWeek(FirstDay) <> 1) ' Find first Sunday
            FirstDay=DateTime.Add (FirstDay,0,0,1)
            offset=offset-1
        Loop 
    Else
        Do While(DateTime.GetDayOfWeek(FirstDay) <> 1) 'Find first Sunday
            FirstDay=DateTime.Add (FirstDay,0,0,-1)
            offset=offset+1
        Loop
    End If
    Return Floor((DateTime.GetDayOfYear(ticks) + offset) / 7) +1
End Sub
code adapted from Erel
 
Last edited:
Upvote 0
Top