Post Reply 
Deltadays and date functions.
05-10-2017, 07:16 PM (This post was last modified: 05-10-2017 07:20 PM by Vtile.)
Post: #11
RE: Deltadays and date functions.
Naturally it started to rain (a snow!) again when I got outside.. So I get in to this today.
(05-10-2017 05:01 PM)Dieter Wrote:  
(05-10-2017 01:28 PM)Vtile Wrote:  I have been in seach of simple algorithm to calculate dDays between two dates.
The JDN method is too heavy to my use (DM15/HP15C)

What? Heavy? A program that works correctly for any valid Gregorian date can be done in less than 80 steps. Take a look at the old HP41 library on this website.
No no no... Not a heavy as is, but judged by the use case. Ie. as a part of financial calculation programs. Or just to check how far the next holiday were. Smile

In general JDN algorithm is really sophisticated and simple (that sounds like I would understand it through).

(05-10-2017 05:01 PM)Dieter Wrote:  
(05-10-2017 01:28 PM)Vtile Wrote:  ...as it needs to be accurate only between about 100 year time window.

In this case things can be simplified a bit. For dates between (and including) 1 Mar 1900 and 28 Feb 2100 every fourth year is a leap year (that's because 2000 is one). This allows to use a simpler formula.
But isn't 2100 a regular year as it is divisable with 100 and 4, but not 400. I must be mixing up something now. (Assuming the RPN program you kindly provided doesn't take somehow account the "regular year if divisable with 100" rule. My RPN still needs to some practice to fluently read it.)

(05-10-2017 05:01 PM)Dieter Wrote:  
(05-10-2017 01:28 PM)Vtile Wrote:  I did found one that suits my use at: https://groups.google.com/forum/#!topic/...c9rcvY7d2s

Hmmm... looks like a few things have been mixed up there. See below.
I see it now, while I still fully understand it.

(05-10-2017 05:01 PM)Dieter Wrote:  
(05-10-2017 01:28 PM)Vtile Wrote:  Now interesting enough I did put 3 different methods to the excel worksheet to compare them simultaneusly.. If I take DDays from 28.2.1900 and 28.2.2100 I get results:

Excel (365): 73050
JDN-JDN^^: 73049
HP50g DDAYS: 73049
SIMPLE DDAYS**: 73050
WAlpha: 73049 ( https://www.wolframalpha.com/input/?i=DA...+28.2.2100 ) PS. As late though

So which ones are correct?

73049 is correct. As usual, Excel cannot be trusted under all circumstances. The date functions only work for a certain range, so you better write your own.
This were suprising, while I did know that it do have quirks in number handling etc. (which I learned from here) I would have assumed it have state of the art calendar functions for accounting and so forth reasons.

(05-10-2017 05:01 PM)Dieter Wrote:  
(05-10-2017 01:28 PM)Vtile Wrote:  ** From mailinglist discussion
Y <- start year
M <- start month
D <- start day
Y <- Y-1
M <- M+13
X <- int(365.25*Y)+int(30.6*M)+D

As already mentioned, some things get mixed up here. For instance an Y and M adjustment has to be applied only for Jan and Feb, and not for all months. For dates between 1 Mar 1900 and 28 Feb 2100 you may use the following method:

Code:
Count Jan and Feb as month 13 and 14 of the previous year:
IF M<3 THEN M=M+12 and Y=Y-1

Then calculate N = INT(Y*365,25) + INT((M+1)*30,6) + D

Δdays = N(date2) - N(date1)
Yes, now I see the function of the 0,25 (1/4) as it is calculated to days with INT/FLOOR only each four years, so it makes the leap year handling in this narrow implementation.

On the other hand I still not get it how the months are handled so sophisticated that the sequence 31,28(**),31,30,31,30,31,31,30,31,30,31 (2+5+5) drops in to that 0.6 with the month shifting IF M<3 THEN M=M+12 and Y=Y-1.. *scratching his head*

**not +1 since that is handled by years multiplier with fraction .25

Well it is 3/5 so it is also found from the JDN algorithm and also from the algorithm mentioned by Don Shepherd.. Somehow it relates to that divisor 5. Well someday I need try to figure how the gears rotate on that one.

(05-10-2017 05:01 PM)Dieter Wrote:  A 15C program can be done in a few minutes:

Code:
001 LBL D
002 STO 0
003 R↓
004 3
005 x<=y?
006 GTO 00
007 +
008 9
009 +
010 ENTER
011 DSE 0
012 LBL 00
013 R↓
014 1
015 +
016 3
017 0
018 ,
019 6
020 x
021 INT
022 3
023 6
024 5
025 ,
026 2
027 5
028 RCLx 00
029 INT
030 +
031 +
032 RTN

This way day [ENTER] month [ENTER] year f[D] returns N(date).
Since the uppermost stack register is preserved, the number of days between two dates can be determined easily:

28 [ENTER] 2 [ENTER] 2100 f[D] => 767146
  1 [ENTER] 3 [ENTER] 1900 f[D] => 694098
  [–] => 73048

Dieter
Do you mind if put a link to this program in my (X)IRR/(X)NPV topic at http://www.hpmuseum.org/forum/thread-8322.html.

Edit. I lost what I were going to edit.. curse of long quote of quote posts.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Deltadays and date functions. - Vtile - 05-10-2017, 01:28 PM
RE: Deltadays and date functions. - Vtile - 05-10-2017, 01:50 PM
RE: Deltadays and date functions. - Vtile - 05-10-2017, 03:02 PM
RE: Deltadays and date functions. - Vtile - 05-10-2017, 07:55 PM
RE: Deltadays and date functions. - Dieter - 05-10-2017, 08:30 PM
RE: Deltadays and date functions. - KeithB - 05-10-2017, 04:30 PM
RE: Deltadays and date functions. - Dieter - 05-10-2017, 05:01 PM
RE: Deltadays and date functions. - Vtile - 05-10-2017 07:16 PM
RE: Deltadays and date functions. - Dieter - 05-10-2017, 08:17 PM
RE: Deltadays and date functions. - Vtile - 05-10-2017, 05:12 PM
RE: Deltadays and date functions. - Dieter - 05-10-2017, 06:17 PM
RE: Deltadays and date functions. - Vtile - 05-11-2017, 11:33 AM
RE: Deltadays and date functions. - Dieter - 05-11-2017, 05:03 PM
RE: Deltadays and date functions. - Vtile - 05-11-2017, 04:17 PM
RE: Deltadays and date functions. - Dieter - 05-12-2017, 08:04 PM
RE: Deltadays and date functions. - Dieter - 05-13-2017, 01:32 PM
RE: Deltadays and date functions. - Dieter - 05-14-2017, 05:21 PM
RE: Deltadays and date functions. - Vtile - 05-12-2017, 11:58 PM
RE: Deltadays and date functions. - Dieter - 05-13-2017, 01:33 PM
RE: Deltadays and date functions. - Vtile - 05-13-2017, 07:15 PM
RE: Deltadays and date functions. - Dieter - 05-13-2017, 08:57 PM
RE: Deltadays and date functions. - Vtile - 05-13-2017, 10:30 PM



User(s) browsing this thread: 1 Guest(s)