Hello,
Struggling here to get the time between to dates:
Can you help please?
Here we see the difference between i and i+2 is 2 hours
How can we take off the init date 1970.01.01 from the result?
thank you in advance.
iTime is a datetime type. the datetime type in mql4/mql5 is in fact an int64 (or long) type and represents the number of seconds elapsed since January 01, 1970
as such if substract two "iTime", you will get the elapsed seconds in between.
if change your last line to
Print("Date i=",Time[i]," Date i+2=",Time[i+2]," Date Diff=",IntegerToString(Time[i]-Time[i+2]));
you'll get
Date i=2021.03.22 13:00:00 Date i+2=2021.03.22 11:00:00 Date Diff=7200
7200 is 2-hour seconds if iTime is applied on an h1-chart.
if this a 5-min chart, the diff will be 2*5*60=600
as the time difference is a long type, use IntegerToString() to convert at the string output.
The difference between 2 dates is in seconds
minutes= difference /60 seconds is the remainder
hours=minutes/60 minutes is the remainder
days=hours/24 hours is the remainder
It works : )
Thank you guys
#define HR0001 (PERIOD_M1*60) #define HR0100 (PERIOD_H1*60) #define HR2400 (PERIOD_D1*60) datetime d1=D'2022.03.04 18:10:00'; datetime d2=D'2022.03.09 17:00:15'; Print("d1 = ",d1,"; d2 = ",d2,"; Difference between two dates: ",ulong(d2-d1)/HR2400," days ",ulong(d2-d1)%HR2400/HR0100," hours ",ulong(d2-d1)%HR0100/HR0001," minutes ",ulong(d2-d1)%HR0001," seconds");
d1 = 2022.03.04 18:10:00; d2 = 2022.03.09 17:00:15; Difference between two dates: 4 days 22 hours 50 minutes 15 seconds
d1 = 2022.03.04 18:10:00; d2 = 2022.03.09 17:00:15; Difference between two dates: 4 days 22 hours 50 minutes 15 seconds
Thanks Andrei!
Could you explane why these value? I understood that are seconds in the period i.e. 3.600 seconds x hour, 86.400 x day, etc....
therefore to get year I could do: (PERIOD_MN1*60) to get 2.592.000 seconds x month and then *12 right?
HR0001 (PERIOD_M1*60) HR0100 (PERIOD_H1*60) HR2400 (PERIOD_D1*60)
And to get also seconds, month and year because I have to coding CAGR https://www.investopedia.com/terms/c/cagr.asp
- www.investopedia.com
Thanks Andrei!
Could you explane why these value? I understood that are seconds in the period i.e. 3.600 seconds x hour, 86.400 x day, etc....
therefore to get year I could do: (PERIOD_MN1*60) to get 2.592.000 seconds x month and then *12 right?
And to get also seconds, month and year because I have to coding CAGR https://www.investopedia.com/terms/c/cagr.asp
this code is not correct.
PERIOD_D1 is not necessarily equal to number of seconds in a day.
Use PeriodSeconds(PERIOD_D1) instead
this code is not correct.
PERIOD_D1 is not necessarily equal to number of seconds in a day.
Use PeriodSeconds(PERIOD_D1) instead
In MQL4 PERIOD_D1 is equal to number of minutes in a day.
But as you say using PeriodSeconds(PERIOD_D1) is much better idea, especially if the coder moves to MQL5 later
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
Struggling here to get the time between to dates:
Can you help please?
Here we see the difference between i and i+2 is 2 hours
How can we take off the init date 1970.01.01 from the result?
thank you in advance.