Re: [amibroker] Re: Number of Days Between Two Dates

 

Hello,

Barry, you are mistaken and all advice given by you is totally incorrect.

You mix up DateNum with DateTime.
These are two completely DIFFERENT things.

DateTime is binary format that is NOT a NUMBER,  so your attempts to use it as a number are plainly and completely wrong.

Your remarks about julian dates are also out of place, because datetimediff calculates difference in SECONDS.

Did you ever bother to read the docs?
http://www.amibroker.com/guide/afl/datetimediff.html

For this reason there is no need to use julian dates.

"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."

Since datetime is NOT a number you can simply put numbers, as you do. This is all wrong.

There is a function to convert form human-readable date to datetime format, it is called StrToDateTime
http://www.amibroker.com/guide/afl/strtodatetime.html

Correct code would be:

startdt = StrToDateTime("2016-04-28");
enddt = StrToDateTime( "2016-12-01");

DifferenceInDays = floor( DateTimeDiff( enddt, startdt )/(24*60*60) );

printf("Difference %g", DifferenceInDays );


And correct answer is 217 days, not  244 as you wrote.

Best regards,
Tomasz Janeczko
amibroker.com

On 2016-04-26 20:40, razzbarry@imageview.us [amibroker] wrote:
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: Tomasz Janeczko <groups@amibroker.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (3)

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