Do Until <date> …/… Loop

  1. 3 months ago

    Emile S

    Jul 8 Europe (France, Strasbourg)

    I am wasting time today and failed on this problem: building code for Sundays only from Date_Start to Date_End in a Do Until loop.

    I use Date_Current as the running date in the loop (it is incremented by 7 days in the loop).

    My method stop on the last Sunday of the end month, not the last Sunday (Date_End).

    Note: in the original code (some years old), the loop ended at the last Sunday of the year instead @ Date_End. This is a progress, but not what I want.
    The code core generate the correct data, only the logic to stop the data generation fails.

    Help !

  2. Hallo Emilie,

    hier meine Scheife für Sonntage = wert 1
    Montag wäre 2

    https://www.dropbox.com/s/ll4687wf67mwi5j/Sonntage1.xojo_binary_project?dl=0

  3. rem sonntage in Listbox
    Dim datum As New date
    Dim m,t As Integer
    listbox1.DeleteAllRows
    datum.year = Val(textfield3.Text)
    For M = 1 To 12
    For T = 1 To 31
    datum.Month = m
    datum.day = T
    If datum.DayOfWeek = 1 Then
    If m=datum.Month Then // 30/ 31 sperren
    listbox1.AddRow datum.LongDate
    End If
    End If
    Next
    Next

  4. @Emile S —This works for me:

    dim Date_Start, Date_End as Date
    
    Date_Start = new Date //Today
    Date_Start.Hour = 0
    Date_Start.Minute = 0
    Date_Start.Second = 0
    
    Date_End = new Date( 2019, 9, 15 )
    
    Dim Current_Date as Date = new Date( Date_Start )
    
    dim oneDay as integer = 24 * 3600 //One day in seconds
    
    //Find first Sunday
    if Current_Date.DayOfWeek>1 then
      Current_Date.TotalSeconds = Current_Date.TotalSeconds + ( 8 - Current_Date.DayOfWeek )*oneDay
    end if
    
    while Current_Date.TotalSeconds <= Date_End.TotalSeconds
      //<<< Do something with date
      
      Current_Date.TotalSeconds = Current_Date.TotalSeconds + 7*oneDay
    wend

    The important thing is to always use the same time, i.e. 00:00:00 here, to avoid "losing" the last Sunday.

  5. Jeff T

    Jul 8 Pre-Release Testers Midlands of England, Europe

    only the logic to stop the data generation fails

    But you don't show your code. And the question is hard to understand.

    If a date loop does not stop when you want it to, the most likely cause is that you are comparing dates for equality.
    But since a date is actually a datetime, you are probably having trouble with times.

    Obtain the dates as SQLDates YYYYMMDD and compare those.
    Make sure that your test is for >= not just =

  6. hallo,

    dayofYear prüfen

    rem sonntage in Listbox
    Dim datum As New date
    Dim start,letztes,m,t As Integer

    listbox1.DeleteAllRows
    datum.year = Val(textfield3.Text)
    datum.Month = Val(startmonat.Text)
    datum.day = Val(starttag.Text)
    start= datum.DayOfYear
    datum.Month = Val(endmonat.Text)
    datum.day = Val(endtag.Text)
    letztes= datum.DayOfYear

    For M = 1 To 12
    For T = 1 To 31
    datum.Month = m
    datum.day = T
    If datum.DayOfWeek = 1 Then
    If m=datum.Month Then // 30/ 31 sperren
    If datum.DayOfYear >= start And datum.DayOfYear <= letztes Then
    listbox1.AddRow datum.LongDate
    End If

    End If
    End If
    Next
    Next

  7. Emile S

    Jul 9 Europe (France, Strasbourg)
    Edited 3 months ago

    Thanks all for your answers…
    (You are right Jeff. Sometimes it is hard to believe what I have on my screen :(
    Not a time trouble. Just a Day / Month / Year thingie)

    The same code who stopped at month end, after I left my local McDonald’s (power down… Power On at home) behaved differently…

    Now, It ends one SUnday after the end date.

    My code was:

    Do Until EndDate.TotalSeconds >= CurrentDate.TotalSeconds
    
    // do some stuff with the date
    
    // Inc the currentDate to next sunday
    CurrentDate.Day = CurrentDate.Day + 7
    
    Loop

    At last, I added:

    If Char_Day > End_Day Then // I will add a + 1 to Char_Day here
          If Char_Month = End_Month Then
            If Char_Year = End_Year Then
              Exit
            End If
          End If
        End If

    Strange Xojo behavior (but I already saw that all these 6+ years).
    [Even after trashing and emptiing (!) it the Xojo cache folder…]

    Edit:
    No, I’m back to my bad behavior (maybe a McDonald’s witch curse ?).

  8. Alberto D

    Jul 9 Pre-Release Testers

    @Emile S My code was:

    Do Until EndDate.TotalSeconds >= CurrentDate.TotalSeconds
    
    // do some stuff with the date
    
    // Inc the currentDate to next sunday
    CurrentDate.Day = CurrentDate.Day + 7
    
    Loop

    This looks weird, if you are adding 7 days to CurrentDate, and you don't change EndDate, testing if EndDate >= CurrentDate should not do anything because since the start EndDate should be bigger than CurrentDate.

    If you have CurrentDate.TotalSeconds >= EndDate.TotalSeconds the only way I can see that it misses the last Sunday is because the time is slightly higher in CurrenDate than EndDate (that's why Stéphane said is important to have the same time)

  9. Emile S

    Jul 9 Europe (France, Strasbourg)

    @Alberto D misses the last Sunday

    It either goes until the end of month or one Sunday too late (End Date was 11-24, I get 12-01 as a gift, once).

    @Alberto D that's why Stéphane said is important to have the same time

    I do not test nor use the time (Hours, Minutes, Seconds…) excepted when I use TotalSeconds. I even tested Day / Month / Years on the Do Until line.

    Maybe I must wait a day or two (or simply made a report into a Listbox), - to clean my brain… a.k.a. forget my current code - before looking at it again (and get new ideas) ?
    I may have a bad design in mind that lock my mind to a dead end.

  10. Alberto D

    Jul 9 Pre-Release Testers

    @Emile S Maybe I must wait a day or two (or simply made a report into a Listbox), - to clean my brain… a.k.a. forget my current code - before looking at it again (and get new ideas) ?
    I may have a bad design in mind that lock my mind to a dead end.

    I was working on the problem exactly like that, filing a Listbox. When I finished I saw Stéphane's code so I didn't post it.

    This is my code (don't look too close to the code, I'm sure it looks weird to a professional programmer):

    Dim Date_Start As New date
    Date_Start.SQLDate = TF_DateStart.Text
    
    Dim Date_End As New date
    Date_End.SQLDate = TF_DateEnd.Text
    
    Dim Date_Current As New date
    Date_Current.SQLDateTime = Date_Start.SQLDateTime
    
    If Date_Current.DayOfWeek <> 1 Then
      Date_Current.Day = Date_Start.Day + (8 - Date_Start.DayOfWeek)
    End
    
    List_Sundays.DeleteAllRows
    // List all Sundays between Date_Start and Date_End
    Do Until Date_Current > Date_End
      List_Sundays.AddRow(Date_Current.SQLDate)
      Date_Current.day = Date_Current.day + 7
    Loop

    Date_Start and Date_End could be any day of the week. Date_Current only list the Sundays between the 2 dates.

    My guess is that your code is changing something.

  11. Emile S

    Jul 9 Europe (France, Strasbourg)

    Thanks Alberto.

    I use two TextFields to get start and end Sundays (I check if the provided dates are Sunday at the start of code). I have set a Date mask n these two TextFields..

    Basically, your code looks like mine. I will check it tomorrow (as I am looking Marvel SHIELD Agents, need some food - dinner - and it will time to sleep).

    @Alberto D My guess is that your code is changing something.

    That is where a "report" into a Listbox comes to debug.

  12. Tim H

    Jul 9 Pre-Release Testers Portland, OR USA

    @Rudolf J For M = 1 To 12
    For T = 1 To 31
    datum.Month = m
    datum.day = T

    You need to be aware that this code will cause some problems. Not sure if they're the one you're experiencing though.

    Xojo dates will auto-adjust when you set the day. So for example if you set Year=2019, Month=2 and Day=29, Xojo will correct that to 2019-03-01. Your code will process some dates more than once.

  13. Emile S

    Jul 9 Europe (France, Strasbourg)

    Tim:
    I discovered yesterday that if I set the day to 0, I get the last day of the previous month.

    I set 2019-03-00 and I get 2019-02-28 ! ;)

    If my memory is correct…

    That said, I knew that, and played with that (adding 7 days to step to the next Sunday) and that part works fine.

    Xojo works fine in some part; it takes good care of Leap years when adding or removing days around end of Febryuary / start of March (when this is a leap year, “tested” in the last hundred years).

    I forgot to say two things:
    That project runs for years without troubles, but usually, I generate a year (January / December) at a time: 52/53 Sundays (or two) and so, my bug was in the shadow, never appears ;).
    I didn’t even tested in the code if the Start / End dates are Sundays prior this last week ! (I set the dates using iCal [is this its macOS US name ?]).

    BTW: my stomach is crying loud and want some meal…

  14. brian f

    Jul 9 Pre-Release Testers, Xojo Pro Chilly California

    @Emile S

    BTW: my stomach is crying loud and want some meal…

    We only eat 1's and 0's around here :P

  15. Emile S

    Jul 9 Europe (France, Strasbourg)

    Hours after eating / sleeping (and very early at night), I awoke.

    I wrote a brand new project with

    Two TextFields (for Start-End Dates),
    Two PushButtons (for Clear Listbox, Go),
    One Listbox (for reporting).

    I populated it with a brand new writed code (below) with few error checkings, save it and run it.

    Code OK / Works fine at first run.

    Now, I will (later) compare the two codes (one in the original project and this one), then copy / paste this code in the other project where I need it. As aften, I was right to stop wasting time (walking in circles to nowhere) and both ask for help / rewrote the code.

     Dim Date_Start As New Date
      Dim Date_End   As New Date
      Dim Date_Now   As New Date
      Dim Loop_Idx   As Integer
      Dim Loc_Row    As Integer
      
      // Fill the Dates Class
      Date_Start.SQLDate = TF_Date_Start.Text
      Date_End.SQLDate   = TF_Date_End.Text
      Date_Now.SQLDate   = TF_Date_Start.Text
      
      Do Until Date_Now > Date_End
        // Add a Sunday “Report”
        LB.AddRow Format(Loop_Idx, "00")
        Loc_Row = LB.LastIndex
        LB.Cell(Loc_Row, 1) = Date_Now.LongDate
        
        // Next Sunday, please
        Date_Now.Day = Date_Now.Day + 7
        
        // Inc the “loop” indice
        Loop_Idx = Loop_Idx + 1
        
        // To avoid an infinite loop
        If UserCancelled Then Exit
      Loop

    Only addition is the Loop_Idx variable, but I could have reported the day of year or week of year…

    Internet is on and the Xojo cache folder was not cleared prior to that :(

    Thanks all for the help.

or Sign Up to reply!