I read more of the paper last night, and it did say to use FLOOR or INT funtions if your language has them. My new formula that I found I have been using the FLOOR function with promising results. Since I could not sleep last night, my coding was a bit rough. Thanks Henko for the changes as the testing I did worked. I was testing using FLOOR and enclosed the entire formula in it, and some cases worked, others did not.
Since I still have a problem running 10/1/2011 to 10/1/2012 (I'm getting 365 instead of 366) Henko's changes made all cases work fine.
Besides Henko's test case, I ran 4 test cases in all, one from Steve, one from Henko and two from me, one to test crossing years with one a leap year to make sure I get 366, and another that goes from 12/1 to 1/1 to make sure I got 31. All worked.
I also applied Rbytes suggestion about using the ABS Function so regardless of the order, you will get a positive number. This has also been included in the test cases, just to make sure something funky doesn't occur trying to subtract like you're going back in time. All cases worked.
Please take the code from this post as it has the test cases and how the ABS() was used.
Again, thank you Henko for the changes. This formula should work, according to the links I provided in my original post, for any date.
However, one caveat — this formula is based on the Gregorian Calendar, so check the links in my other post on this calendar system, as one of the links shows when countries adopted it. If you are checking dates before the calendar was applied, another formula for the Julian Calendar will have to be used, and if you are checking dates that cross both calendars, I will need to work on merging them.
In cosmology, we use a different calendar, the Solar Calendar, and I have written code on the mainframe that converts it to a calendar date, but its written in COBOL and will take a crap load of work to convert it to basic (why I didn't code it in PL/1 or FORTRAN I'll never know
).
Let me know if you find any other problems with this, as I plan on using it in my code from this point on.
George.
George
Henko wrote: ↑Mon Feb 05, 2018 12:22 pm
My last contribution to this thread:
Here is George's formula how it should be coded in SmartBasic (imho).
Please test it.
*** NOTE FROM GEORGE: Tested and I replaced the code below with the four test cases, the ABS suggestion. So copy the code from here.
Code: Select all
REM Difference between two dates = g(y2,m2,d2) - g(y1,m1,d1)
PRINT "*** RUN USING THE NEW FORMULA"
PRINT
PRINT "FIRST RUN - 1/21/2018 TO 2/3/2018, THE RESULT SHOULD BE 13"
PRINT ABS(g(2018,2,3) - g(2018,1,21))
PRINT ABS(g(2018,1,21) - g(2018,2,3))
PRINT
PRINT "SECOND RUN - 12/1/2017 TO 1/1/2018, THE RESULT SHOULD BE 31"
PRINT ABS(g(2018,1,1) - g(2017,12,1))
PRINT ABS(g(2017,12,1) - g(2018,1,1))
PRINT
PRINT "THIRD RUN - 10/1/2011 TO 10/1/2012, THE RESULT SHOULD BE 366"
PRINT ABS(g(2012,10,1) - g(2011,10,1))
PRINT ABS(g(2011,10,1) - g(2012,10,1))
PRINT
PRINT "FOURTH RUN - 2/3/2016 TO 2/3/2017, THE RESULT SHOULD BE 366"
PRINT ABS(g(2017,2,3) - g(2016,2,3))
PRINT ABS(g(2016,2,3) - g(2017,2,3))
DEF g(y,m,d)
m=(m+9)%12
y=y-floor(m/10)
nd=365*y+floor(y/4)-floor(y/100)+floor(y/400)+floor((m*306+5)/10)+(d-1)
RETURN nd
ENDDEF