[amibroker] Re: Number of Days Between Two Dates

 

This is much more complicated than you may think, Your calculations must factor in leap years and periods that cross years. It would be much easier to google convert date to Julian and then simply subtract the Julian dates to find the number of days.


If you must do this in your program then code a function that converts the date to julian. Then call it twice with different dates and subtract. The java code is at the link below and can be converted to afl with some effort. 


Don't use now. datenum returns the date as yyymmdd. You can see this using 

printf(StrFormat("Date %f", (lastvalue(datenum() ) ) ) );

This prints Date 1160426.000000 Use int(date) to truncate the fraction,

You cannot simply subtract two datenum values to get the number of days,  1161201 - 1160428 = 781 days, oops. It should be 244 days.
 
I tried using days = DateTimeDiff(1161201000000, 1160428000000); to get the number of days between 4/28 and 12/1 2016 and got an incorrect value. If DateTimeDiff can be used to get the number of days I cannot figure out how to code it. The description says it returns the difference in seconds but dividing by 3600 isn't correct either.

If you convert he java this will return the number of days in a specified month,

function fDaysInMonth(m) // find days in month
{
n = 30; // num Days In Month default to 30
if(m == 2) // get days in Feb
{
y = LastValue(Year());
if ( frac(y / 100) != 0 AND frac(y / 4) == 0     OR // if evenly divisible by 4 and not 100 like 2912 then is leap year
   ( frac(y / 100) == 0 AND frac(y / 400) == 0 ) )  n = 29; // if evenly divisible by 25 then is leap year
else n = 28;
}
else if(m == 1 OR m == 3 OR m == 5 OR m == 7 OR m == 8 OR m == 10 OR m == 12) n = 31;
return n;
}

Cheers,
Barry




 

 

__._,_.___

Posted by: razzbarry@imageview.us
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (2)

Upgrade your account with the latest Yahoo Mail app
Get organized with the fast and easy-to-use Yahoo Mail app. Upgrade today!

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/


.

__,_._,___


EmoticonEmoticon