Help with date math

I am working on an app that is counting down to a student’s test date. How is this best accomplished? I am using this below, but if the test date is in 2017 or further, this treats the days until the test as if it were the same year

d.Day = rs3.Field("MyExamDate").DateValue.DayOfYear - d.Day

If I choose a test date of 2016-08-15, then the days until is showing as 6 (probably figuring in today). When I choose 2017-08-15 (next year), it shows 5 days until, so this is not right.

What is the setup to account for future years and count the number of days?

Also, I am using the Calendar Picker from the examples. Is there a way to not choose a date earlier than today?

Use TotalSeconds to calculate the difference in the dates and then use the date object to make it readable.

dim dtDaysRemain as Date
dim dtToday as new Date
dtDaysRemain.TotalSeconds = dtTestDate.TotalSeconds - dtToday.TotalSeconds

You can then use dtDaysRemain to get the readable stuff like Days.

Tim, you method will return a DATE not # of days (unless it happens to be less than 31)…
32 days would be something like “01-Feb-1970”

dim daysRemaining as integer
daysRemaining = ceil((dtTestDate.TotalSeconds - dtToday.TotalSeconds)/(24*60*60))

You rock Dave! Works like a charm!

Ah well, I pulled it from memory.
I see that it wouldn’t work for any timeframe longer than a month.

Just for the record, for anybody interested in the subject of time difference, there was a very long thread a couple years ago, which among other things, showed the difference between counting days in Europe and in the US.

https://forum.xojo.com/15374-getting-the-date-difference-in-years-months-days/0

This is how you could do it using the new framework:

Dim testDate As New Xojo.Core.Date(2017, 8, 15, Xojo.Core.TimeZone.Current)
Dim interval As Xojo.Core.DateInterval
interval = testDate - Xojo.Core.Date.Now
  
MsgBox("The test is " + interval.Years.ToText + " years, " + interval.Days.ToText + " days and " + interval.Hours.ToText + " hours from now.")