Search code examples
mql5metatrader5

zero is omitted while converting integer to string MQL5


See the following code I tried:

MqlDateTime time;
TimeToStruct(TimeCurrent(),time);
string currenttime=IntegerToString(time.year)+"-0"+
                      IntegerToString(time.mon)+"-"+
                      IntegerToString(time.day)+ " "+
                      IntegerToString(time.hour)+ ":"+
                      IntegerToString(time.min)+ ":00";

The output of the following was: 2018-7-25 15:8:00 where I was expecting the output as 2018-07-25 15:08:00, that is in 2 values the month and the min. But it is not converting the zero. The date time what I am giving is in the format 2018-07-25 15:08:00, which is I am expecting to get after converting to string. But got something else.

Please help me.


Solution

  • Why do you think the minutes should have zero before it? according to the code you have, seems minutes are not without leading zero.

    Anyway if you do not like TimeToString, try StringFormat.

    string output=StringFormat("%04d-%02d-%02d %02d:%02d:00", 
                   time.year,time.mon,time.day,time.hour,time.min);