Help please - date math

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Help please - date math

Post by Henko »

Henko wrote:
Sun Feb 04, 2018 8:57 am
George,

Are you sure about your little code?
In the website you refer to, it is said that all divisions are integer divisions. In your code they are not. There's an INT for the end result, but i think each individual division should be INT'ed.
Also bear in mind that in SB, INT means rounding and not truncation.
George, Steve,
Apparently both of you missed my earlier post in this thread. It points out where the calculation may go wrong.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Help please - date math

Post by rbytes »

I tried adding a separate INT to each division. It didn't fix the problem.
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 848
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Help please - date math

Post by Dutchman »

Henko wrote:
Sun Feb 04, 2018 3:32 pm
We know, and rbyres started to point to your library. Steve tried it and could not succeed with it.
So, just pointing at it a second time will not be very helpfull 😉.
You refer to the library, but apparently you did not run the program "Date Calculator.txt".
It gives the period between dates or it gives the date before or after a period from a date.
In the program header the following examples are given:
Examples:
2012-09-24 gives characteristics of Monday 24 September 2012
2012-09-24 2012-w39-1 gives an interval of 0 days, dates are equal
2012-09-24 +38w gives the date of an average human gestation period further
+500m 20120924 gives the date 500 months further
-100d 2012W391 gives the date 100 days back
20120924 20110924 gives an interval of -366 days (leapyear)
Today +38w gives the date 38 weeks from today
The following screenshot is an example of entering two dates:
two dates.PNG
two dates.PNG (124.23 KiB) Viewed 4711 times
You can enter the current date as "today".
The following example calculates the date after a certain period:
a period from today.PNG
a period from today.PNG (109.65 KiB) Viewed 4711 times

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Help please - date math

Post by GeorgeMcGinn »

It will not, because enclosing the entire formula in parentheses, it makes each division obey the INT, or more accurately, it applies it to each division.

I wrote a slightly different formula, using another method I know about, incorporating the number of days since the start of the Gregorian Calendar, and I get 13, not 14.

The problem is if you ask for the number of days between 1/21/2018 and 2/3/2018, 13 is correct as you want the number of days BETWEEN, which means you must exclude both start and end dates. (Don't believe me, use a real calendar and count the days between and you get 13)

Programming, you must exactly specify what the problem is.

However, if you want the number of days from 1/21/2018 to 2/3/2018, then both start and end dates must be included, as it is a from/to problem. The number of days in that case is really 15.

Bish99, how do you figure 14? The only way that works is if you do not count one of the days. What is the exact spec/wording to come up with that result? It would be something like you want the number of days from 1/21/2018 to 2/3/2018. That will give younthe number of days of 14, as you would actually not include 2/3/2018 in the calculation. You would need to use the date 2/2/2018 to get 14.

Rbytes is correct. Using the ABS Function will eliminate the need to do the subtraction in a specific order. I have tried many combinations, and always got the same negative value reversing the dates in the equation.

Tomorrow I will pick up on why the original formula isn't working, as I read the two links and I am using the formula properly, and my function, besides using the INT Function, is an exact match. I will speak/email some other mathematicians about why these formulas do not work all the time. They are in journal papers and should work properly.


rbytes wrote:
Mon Feb 05, 2018 6:42 am
I tried adding a separate INT to each division. It didn't fix the problem.
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Help please - date math

Post by GeorgeMcGinn »

I found it, but it was later. Sorry about that.
Henko wrote:
Mon Feb 05, 2018 6:36 am
Henko wrote:
Sun Feb 04, 2018 8:57 am
George,

Are you sure about your little code?
In the website you refer to, it is said that all divisions are integer divisions. In your code they are not. There's an INT for the end result, but i think each individual division should be INT'ed.
Also bear in mind that in SB, INT means rounding and not truncation.
George, Steve,
Apparently both of you missed my earlier post in this thread. It points out where the calculation may go wrong.
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Help please - date math

Post by GeorgeMcGinn »

Steve,

Using Dutchman's Date Calculator program you get 13 days between your dates, not 14.

I found another formula from a paper in a math journal that gives me 13 as well, but I am having problems when the interval goes over a year that is a leap year. I will have that fixed tomorrow.

Below are screen prints from running your dates using Dutchman's program.

If you are looking to include one or both of your dates, you need to explain why.

IMG_0045.JPG
IMG_0045.JPG (183.78 KiB) Viewed 4706 times
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Help please - date math

Post by GeorgeMcGinn »

Dutchman, I was looking through your code and I found an error, which I do not think will effect the results. The Gregorian Calendar actually starts on Thursday, 14 September 1752 in the United States, not on 15 October 1582.

This is one of the many issues that effect calculating dates. While countries in Europe adopted the calendar at different times, anyone who is looking to do historical dating needs to research this, otherwise if I were to use this program for interval dates or to determine the day of the week, US took almost 200 years (or 62,062 days) to adopt the Gregorian Calendar and you will not get correct results.

For example, this program says 2 September 1752 was a Saturday, 62,050 day of the Gregorian Calendar. It was actualy a Wednesday, a day before the US Implemented the new calendar (see image).

Calculating dates is serious but cannot be applied to every situation or for all countries equally.

Here is a comprehensive site comparing the Julian and Gregorian calendars: https://www.timeanddate.com/calendar/ju ... witch.html

Here is a link from the History Channel about the Gregorian Calendar: http://www.history.com/news/6-things-yo ... n-calendar
Attachments
IMG_0048.PNG
IMG_0048.PNG (489.17 KiB) Viewed 4699 times
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Help please - date math

Post by Henko »

My last contribution to this thread:
Here is George's formula how it should be coded in SmartBasic (imho).
Please test it.

Code: Select all

PRINT g(2017,2,3) - g(2016,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

REM Difference between two dates = g(y2,m2,d2) - g(y1,m1,d1)

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Help please - date math

Post by GeorgeMcGinn »

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
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Help please - date math

Post by rbytes »

The latest code posts by Henk and George are producing correct intervals in all of my tests. Well done, all! :D
The only thing that gets me down is gravity...

Post Reply