Android Question find Friday between the two dates

sigster

Active Member
Licensed User
Longtime User
Hi


How can I find Friday between the two dates 19 and 25 January.

Regards
Sigster
 
Solution
B4X:
Sub FirstDayOfIcelandicMonth (MonthName As String, Year As Int) As String
    DateTime.DateFormat = "dd/MM/yyyy"
    Select Case MonthName
        Case "Þorri"
            Dim Jan19 As Long = DateTime.DateParse("19/01/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Jan19)
            Dim P As Period = CalculatePeriod(DayOfWeek, 6)
            Dim FirstFriday As Long = DateUtils.AddPeriod(Jan19, P)
            Return DateTime.Date(FirstFriday) & " (Friday)"
        Case "Góa"
            Dim Feb18 As Long = DateTime.DateParse("18/02/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Feb18)
            Dim P As Period = CalculatePeriod(DayOfWeek, 1)
            Dim FirstSunday As Long =...

sn_nn

Member
Licensed User
Longtime User
Try this algorithm:
This code calculates DaysCount from 1.1.1 (NewAge) and takes 7-cycle index for weekday of date
Use this procedure from one date to second date and check weekday!

Day of Week:
Sub Day_of_Week (Day as int, Month as int,Year as int) as int
Dim res as int
if Month<3 then
   Month=Month+12
   Year=Year-1
End if
Dim Days as long 'Days count from NewAge up to OurDate
Days=Trunc((Мonth+1)*30,6)+Trunc(Year*365,25)+(Day)-114
res=(Days-14) Mod 7
'0-sunday, 1-monday et.c.
Return res
End sub

'Get Integer part of Number
Sub Trunc(arg As Double) As Long
Dim Res As Long
If arg>=0 Then Res=Floor(arg) Else Res=Ceil(arg)
Return Res
End Sub
 
Upvote 0

spsp

Active Member
Licensed User
Longtime User
Hello,

other way :

B4X:
Private Sub next_specific_day(d1 as long,d2 as long,dow as int) as long
    Dim add As Int=dow-DateTime.GetDayOfWeek(d1)
    if add<=0 then
       add=add+7
    end if
    Dim r As Long=DateTime.Add(d1,0,0,add)
    If r<=d2 Then
        return r
    Else
        return 0
    End If
End Sub


spsp
 
Last edited:
Upvote 1

emexes

Expert
Licensed User
This code calculates DaysCount from 1.1.1 (NewAge) and takes 7-cycle index for weekday of date

Why not just start counting day-by-day from the first date (19-Jan-2022?) until weekday is Friday? Maximum of 6 counts.

Or if that potential inefficiency drives you nuts, then add (desired weekday number - weekday number of first date + 7) mod 7 days to the first date?

Eg if first date is weekday 3 and desired weekday is 5, then add 2 days.

I just realised this is effectively what @spsp wrote, except using modulus instead of if-then-add to handle day-of-week wraparound. ☮️

edit: I think mod handles negative cases ok, but was simpler to add 7 anyway, to be sure, to be sure. :)
 
Upvote 0

PaulMeuris

Active Member
Licensed User
You could use the DateTime methods and the DateUtils library like so:
B4X:
    Private firstticks As Long = DateUtils.SetDate(2022,1,19)
    Private dayticks As Long = DateTime.TicksPerDay
    Private currticks As Long = firstticks
    Do While DateTime.GetDayOfWeek(currticks) <> 6        ' 1 = sunday - 7 = saturday
        currticks = currticks + dayticks
    Loop
    Private year As Int = DateTime.GetYear(currticks)
    Private month As Int = DateTime.GetMonth(currticks)
    Private day As Int = DateTime.GetDayOfMonth(currticks)
    DateTime.DateFormat = "dd/MM/yyyy"
    Private fridaydate As String = day & "/" & month & "/" & year
    Log(fridaydate)
This code also gives the correct result when the year changes. Try it with (2022,12,31).
 
Upvote 0

sigster

Active Member
Licensed User
Longtime User
Maybe I explain better what I am try to do
this is old month name I am try to add to date widget so 19-25 January is from Friday to Tuesday in this year 2022

Sample first 3 month
Þorri - Starts on Friday in the 13th week of winter between 19 and 25 January.
Góa - Starts on Sunday in the 18th week of winter between 18 and 24 February.
One month - Starts on Tuesday in the 22nd week of winter between March 20 and 26.

Regards
Sigster
date-count.png
 
Upvote 0

emexes

Expert
Licensed User
Þorri - Starts on Friday in the 13th week of winter between 19 and 25 January.
Góa - Starts on Sunday in the 18th week of winter between 18 and 24 February.
One month - Starts on Tuesday in the 22nd week of winter between March 20 and 26.

Use @PaulMeuris example to calculate the start dates, then add 6 lots of DateTime.TicksPerDay to the start dates to calculate the end dates of the 7-day weeks.

Note that finding the Friday between 19 and 25 January is same as finding the first Friday on or after 19 January.

The end date (25 January) is not needed and makes no difference to the result.

Although perhaps it is nice double-check for manual calculations, eg if our result is 29 January then we know we've made a mistake.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I believe "one month" is Einmánuðr the first day of the last month of winter. Definitely a good thing in Iceland.
If Iceland is like Canada where I am, people will be outside drinking beer on patios (albeit in winter gear).

@emexes strategy will work for all of the targets for any year.🍻

Start at the target day
Þorri - Start on January 19 and look for first Friday (could be the 19th)
Góa - Start on February 18 and look for first Sunday (could be the 18th)
Einmánuðr - Start on March 20 and look for first Tuesday (could be the 20th)

These are all very short (nano second) loops.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Sub FirstDayOfIcelandicMonth (MonthName As String, Year As Int) As String
    DateTime.DateFormat = "dd/MM/yyyy"
    Select Case MonthName
        Case "Þorri"
            Dim Jan19 As Long = DateTime.DateParse("19/01/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Jan19)
            Dim P As Period = CalculatePeriod(DayOfWeek, 6)
            Dim FirstFriday As Long = DateUtils.AddPeriod(Jan19, P)
            Return DateTime.Date(FirstFriday) & " (Friday)"
        Case "Góa"
            Dim Feb18 As Long = DateTime.DateParse("18/02/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Feb18)
            Dim P As Period = CalculatePeriod(DayOfWeek, 1)
            Dim FirstSunday As Long = DateUtils.AddPeriod(Feb18, P)
            Return DateTime.Date(FirstSunday) & " (Sunday)"
        Case "Einmánuður"
            Dim Mar20 As Long = DateTime.DateParse("20/03/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Mar20)
            Dim P As Period = CalculatePeriod(DayOfWeek, 3)
            Dim FirstTuesday As Long = DateUtils.AddPeriod(Mar20, P)
            Return DateTime.Date(FirstTuesday) & " (Tuesday)"
        Case "Harpa"
            Dim Apr18 As Long = DateTime.DateParse("18/04/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Apr18)
            Dim P As Period = CalculatePeriod(DayOfWeek, 5)
            Dim FirstThursday As Long = DateUtils.AddPeriod(Apr18, P)
            Return DateTime.Date(FirstThursday) & " (Thursday)"
        Case "Skerpla"
            Dim May19 As Long = DateTime.DateParse("19/05/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(May19)
            Dim P As Period = CalculatePeriod(DayOfWeek, 7)
            Dim FirstSaturday As Long = DateUtils.AddPeriod(May19, P)
            Return DateTime.Date(FirstSaturday) & " (Saturday)"
        Case "Sólmánuður"
            Dim Jun18 As Long = DateTime.DateParse("18/06/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Jun18)
            Dim P As Period = CalculatePeriod(DayOfWeek, 2)
            Dim FirstMonday As Long = DateUtils.AddPeriod(Jun18, P)
            Return DateTime.Date(FirstMonday) & " (Monday)"
        Case "Heyannir"
            Dim Jul23 As Long = DateTime.DateParse("23/07/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Jul23)
            Dim P As Period = CalculatePeriod(DayOfWeek, 1)
            Dim FirstSunday As Long = DateUtils.AddPeriod(Jul23, P)
            Return DateTime.Date(FirstSunday) & " (Sunday)"
        Case "Tvímánuður"
            Dim Aug22 As Long = DateTime.DateParse("22/08/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Aug22)
            Dim P As Period = CalculatePeriod(DayOfWeek, 3)
            Dim FirstTuesday As Long = DateUtils.AddPeriod(Aug22, P)
            Return DateTime.Date(FirstTuesday) & " (Tuesday)"
        Case "Haustmánuður"
            Dim Sep20 As Long = DateTime.DateParse("20/09/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Sep20)
            Dim P As Period = CalculatePeriod(DayOfWeek, 5)
            Dim FirstThursday As Long = DateUtils.AddPeriod(Sep20, P)
            Return DateTime.Date(FirstThursday) & " (Thursday)"
        Case "Gormánuður"
            Dim Oct21 As Long = DateTime.DateParse("21/10/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Oct21)
            Dim P As Period = CalculatePeriod(DayOfWeek, 7)
            Dim FirstSaturday As Long = DateUtils.AddPeriod(Oct21, P)
            Return DateTime.Date(FirstSaturday) & " (Saturday)"
        Case "Ýlir"
            Dim Nov20 As Long = DateTime.DateParse("20/11/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Nov20)
            Dim P As Period = CalculatePeriod(DayOfWeek, 2)
            Dim FirstMonday As Long = DateUtils.AddPeriod(Nov20, P)
            Return DateTime.Date(FirstMonday) & " (Monday)"
        Case "Mörsugur"
            Dim Dec20 As Long = DateTime.DateParse("20/12/" & Year)
            Dim DayOfWeek As Int = DateTime.GetDayOfWeek(Dec20)
            Dim P As Period = CalculatePeriod(DayOfWeek, 4)
            Dim FirstWednesday As Long = DateUtils.AddPeriod(Dec20, P)
            Return DateTime.Date(FirstWednesday) & " (Wednesday)"
        Case Else
            Return ""
    End Select
End Sub

' DayOfWeek1 = DayOfWeek of the start date
' DayOfWeek2 = DayOfWeek to look for
Sub CalculatePeriod (DayOfWeek1 As Int, DayOfWeek2 As Int) As Period
    Dim P As Period
    Select Case DayOfWeek2
        Case 1 ' Sunday
            P.Days = 8 - DayOfWeek1
            If P.Days = 7 Then P.Days = 0
        Case 2 ' Monday
            P.Days = 9 - DayOfWeek1
            If P.Days > 6 Then P.Days = P.Days - 7
        Case 3 ' Tuesday       
            If DayOfWeek1 > 3 Then
                P.Days = 10 - DayOfWeek1
            Else
                P.Days = 3 - DayOfWeek1
            End If
        Case 4 ' Wednesday   
            If DayOfWeek1 > 4 Then
                P.Days = 11 - DayOfWeek1
            Else
                P.Days = 4 - DayOfWeek1
            End If
        Case 5 ' Thursday
            P.Days = 5 - DayOfWeek1
            If P.Days < 0 Then P.Days = 7 + P.Days
        Case 6 ' Friday
            P.Days = 6 - DayOfWeek1
            If P.Days < 0 Then P.Days = DayOfWeek1 + P.Days
        Case 7 ' Saturday
            P.Days = 7 - DayOfWeek1
            If P.Days = 7 Then P.Days = 0
    End Select
    Return P
End Sub

Usage:
B4X:
Log( FirstDayOfIcelandicMonth("Þorri", 2022) )        ' 21/01/2022 (Friday)
Log( FirstDayOfIcelandicMonth("Góa", 2022) )          ' 20/02/2022 (Sunday)
Log( FirstDayOfIcelandicMonth("Einmánuður", 2022) )   ' 22/03/2022 (Tuesday)
Log( FirstDayOfIcelandicMonth("Harpa", 2022) )        ' 21/04/2022 (Thursday)
 
Upvote 0
Solution
Top