Make Hour = hh:mm not hh

To add comments, please log in or register
bigfatgreedykat
19
bigfatgreedykat  

Hi 

I have an extern int opentime

Above is used to enter a time.

This time is checked against another 'if' statement

if ( Hour() == opentime )

Above works but 'Hour' can only be entered as two digit number i.e. 23 or 04

I want to enter the time as 23:46 or 04:57 

Can anyone point me in the right direction, thanks

regards

Jai

whroeder1
17944
whroeder1  
  1. Perhaps you should read the manual.
              Conversion Functions / StringToTime - Reference on algorithmic/automated trading language for MetaTrader 5
  2. Of course if you do that, you won't be able to use Hour.
soubra2003
31839
soubra2003  

by opening a job and hiring a developer you will get a professional work coded to you

then, you will keep it saved to your library



https://www.mql5.com/en/job/new

Marco vd Heijden
Moderator
8163
Marco vd Heijden  

Please see 

TimeToString

Converting a value containing time in seconds elapsed since 01.01.1970 into a string of "yyyy.mm.dd hh:mi" format.

string  TimeToString( 
   datetime  value,                           // number 
   int       mode=TIME_DATE|TIME_MINUTES      // output format 
   );

Parameters

value

[in]  Time in seconds from 00:00 1970/01/01.

mode=TIME_DATE|TIME_MINUTES

[in] Additional data input mode. Can be one or combined flag: 

TIME_DATE gets result as "yyyy.mm.dd", 
TIME_MINUTES gets result as "hh:mi", 
TIME_SECONDS gets results as "hh:mi:ss".

Return Value

String.

https://www.mql5.com/en/docs/convert/timetostring
Documentation on MQL5: Conversion Functions / TimeToString
Documentation on MQL5: Conversion Functions / TimeToString
  • www.mql5.com
Conversion Functions / TimeToString - Reference on algorithmic/automated trading language for MetaTrader 5
nicholi shen
1874
nicholi shen  

It sounds like you would like to use a string input so you can type the exact time in minutes and hours, correct? If so then this is an example of how you could implement it. 


input string TimeInput = "20:45";
void OnStart()
{
   Print(CompareTime(TimeInput));
}
//+------------------------------------------------------------------+

bool CompareTime(string time_str)
{
   datetime time = StringToTime(time_str);
   MqlDateTime t1,t2;
   TimeToStruct(time,t1);
   TimeCurrent(t2);
   if(t1.hour==t2.hour && t1.min==t2.min)
      return true;
   return false;
}

To add comments, please log in or register