How to effectively add a "0" before the hour or minute to a string variable for displaying the time, when using "MqlDateTime" variable?

 

Hi I use some code for showing the current and past time when my EA performs an action or event. 

It converts the time from a MqlDateTime variable to a string variable with this format: "HH:MM"

When the hour or minute is before 10, it adds an extra "0" to the string, see the code:

      Info.sTimePeriod // = struct for storing the time in a string format.

      MqlDateTime TimePeriod; // MQL date/time structure 

// Convert current PC time to MQL time format. 
      TimeLocal(TimePeriod);

// Stringvalue for time expression in standard time method.
      if(TimePeriod.hour < 10) 
            {
            Info.sTimePeriod = (string)0 + (string)TimePeriod.hour;
            } 
            else 
            {
            Info.sTimePeriod = (string)TimePeriod.hour;
            }
            
      Info.sTimePeriod = Info.sTimePeriod + ":";
      
      if(MathRound(dMinTime_Period) < 10) 
            {
            Info.sTimePeriod = Info.sTimePeriod + (string)0 + (string)MathRound(dMinTime_Period);
            } 
            else 
            {
            Info.sTimePeriod = Info.sTimePeriod + (string)MathRound(dMinTime_Period);
            }  

Although this code works. Its is very clumsy and I think that it can be done more simple by using the StringFormat() function but I don't know how to use this function.

https://www.mql5.com/en/docs/convert/stringformat

Can anybody help me?


Thanks.

Documentation on MQL5: Conversion Functions / StringFormat
Documentation on MQL5: Conversion Functions / StringFormat
  • www.mql5.com
Conversion Functions / StringFormat - Reference on algorithmic/automated trading language for MetaTrader 5
 
printf("%s",TimeToString(D'2016.04.16 9:5',TIME_MINUTES));      
 

Thanks Alain for your suggestion.

I have solved the problem with this code:

Info.sTimePeriod = StringFormat("%i = %02i:%02i", Info.nCurrent_ModelTime, TimePeriod.hour, int(dMinTime_Period));