Time between 2 dates

 

Hello,


Struggling here to get the time between to dates:

Can you help please?

Print("Date i=",Time[i]," Date ZZ4=",Time[ZZt[4]]," Date Diff=",Time[i]-Time[ZZt[4]]);
// => Date i=2021.03.09 17:00:00 Date ZZ4=2021.03.04 18:00:00 Date Diff=1970.01.05 23:00:00

Print("Date i=",Time[i]," Date ZZ4=",Time[ZZt[4]]," Date Diff=",(Time[i]-Time[ZZt[4]])/60);
// => Date i=2021.03.09 17:00:00 Date ZZ4=2021.03.04 18:00:00 Date Diff=1970.01.01 01:59:00


Here we see the difference between i and i+2 is 2 hours

Print("Date i=",Time[i]," Date i+2=",Time[i+2]," Date Diff=",Time[i]-Time[i+2] );
// Date i=2021.03.04 18:00:00 Date i+2=2021.03.04 16:00:00 Date Diff=1970.01.01 02:00:00


How can we take off the init date 1970.01.01 from the result?


thank you in advance.

 
Baptiste George:

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

 
Andrei Iakovlev #:

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

Compound Annual Growth Rate (CAGR) Formula and Calculation
Compound Annual Growth Rate (CAGR) Formula and Calculation
  • www.investopedia.com
The compound annual growth rate (CAGR) is the rate of return (RoR) that would be required for an investment to grow from its beginning balance to its ending balance, assuming the profits were reinvested at the end of each period of the investment’s life span. Key Takeaways The compounded annual growth rate (CAGR) is one of the most accurate...
 
Milko Vivaldi #:

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

 
Andrey Barinov #:

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

 
Keith Watford #:

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

It is equal today. But no guarantees about tomorrow:)
Reason: