Do Until <date> …/… Loop

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 !

Hallo Emilie,

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

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

@Emile Schwarz —This works for me:

[code]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

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

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 =


dayofYear prüfen

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

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

For M = 1 To 12
For T = 1 To 31
datum.Month = m = 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


Thanks all for your answers…
(You are right Jeff. Sometimes it is hard to believe what I have on my screen :frowning:
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:

[code]Do Until EndDate.TotalSeconds >= CurrentDate.TotalSeconds

// do some stuff with the date

// Inc the currentDate to next sunday
CurrentDate.Day = CurrentDate.Day + 7


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…]

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

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)

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).

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.

[quote=444806:@Emile Schwarz]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.[/quote]
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):

[code]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)

// List all Sundays between Date_Start and Date_End
Do Until Date_Current > Date_End
List_Sundays.AddRow(Date_Current.SQLDate) = + 7
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.

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).

That is where a “report” into a Listbox comes to debug.

[quote=444692:@Rudolf Jackel]For M = 1 To 12
For T = 1 To 31
datum.Month = m = T[/quote]
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.

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 ! :wink:

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…

[quote=444814:@Emile Schwarz]

BTW: my stomach is crying loud and want some meal…[/quote]

We only eat 1’s and 0’s around here :stuck_out_tongue:

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.

[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


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

Thanks all for the help.