# Substracting hours from MQLDateTime

133

Hi,

Given a nb of hours that may be like 4, 235, or 0.4 hours, I want to get the MQLDateTime at that time back.

For example: GMT time now is 4.40 am, so 4 hours back it was 0.40.

I made a function that gives good answers if given numbers like 4, 234, but fail when given a number like 0.4:

```//+------------------------------------------------------------------+
//| MQL function to get the time back x hours                        |
//+------------------------------------------------------------------+
MqlDateTime EquivalentBackHour(double nbHoursBack)
{
// First get actual hour and turn it GMT:
MqlDateTime T_Now; TimeGMT(T_Now);
// Then transform the nbHoursBack into seconds:
int nbSecondsBack = (int) nbHoursBack * 3600;
// Then substract seconds from seconds
datetime BackTimeInSeconds =(StructToTime(T_Now)-nbSecondsBack);
// Then transform that datetime in seconds to MQL5 structure:
MqlDateTime T_Back; TimeToStruct(BackTimeInSeconds, T_Back);
Print ("Time Now  GMT = " + IntegerToString(T_Now.hour)  + " " + IntegerToString(T_Now.min));
Print ("Time Back GMT = " + IntegerToString(T_Back.hour) + " " + IntegerToString(T_Back.min) + " for " + DoubleToString(nbHoursBack,2) + " hours back");
return T_Back;
}```

Time Now  GMT = 11 31 >> Time Back GMT = 6 31   for 5.00 hours back         (True)

Time Now  GMT = 11 31 >> Time Back GMT = 11 31 for 0.26 hours back         (Wrong)

Time Now  GMT = 11 31 >> Time Back GMT = 23 31  for 60.00 hours back      (True)

Any help?

1061

(int) (nbHoursBack*3600)
17925

`int nbSecondsBack = (int) nbHoursBack * 3600;`

When nbHoursBack is 0.26 you compute 0 * 3600
Language Basics / Operations and Expressions / Precedence Rules - Reference on algorithmic/automated trading language for MetaTrader 5

`int nbSecondsBack = int( nbHoursBack * 3600 );`
133

Thanks Kypa! I thought it was harmless to make nbSecondsBack into integer, since it can not hold fraction part (as 3600 is big enough)

```//+------------------------------------------------------------------+
//| MQL function to get the time back x hours                        |
//+------------------------------------------------------------------+
MqlDateTime EquivalentBackHour(double nbHoursBack)
{
// First get actual hour and turn it GMT:
MqlDateTime T_Now; TimeGMT(T_Now);
// Then transform the nbHoursBack into seconds:
double nbSecondsBack =  nbHoursBack * 3600;
// Then substract seconds from seconds
datetime BackTimeInSeconds = (datetime) (StructToTime(T_Now)-nbSecondsBack);
// Then transform that datetime in seconds to MQL5 structure:
MqlDateTime T_Back; TimeToStruct(BackTimeInSeconds, T_Back);
Print ("Time Now  GMT = " + IntegerToString(T_Now.hour)  + " " + IntegerToString(T_Now.min));
Print ("Time Back GMT = " + IntegerToString(T_Back.hour) + " " + IntegerToString(T_Back.min) + " for " + DoubleToString(nbHoursBack,2) + " hours back");
return T_Back;
}```
whroeder1 also thanks, lesson learned about (int): I never thought it behaves like a normal operator.
330

Hello, did you try to change datetime BackTimeInSeconds in MqlDateTime BackTimeInSeconds ? I'm not sure but it's the only difference I noticed.

Good Luck! Let us know when you arrange it !