zero is omitted while converting integer to string

To add comments, please log in or register
jaffer wilson
747
jaffer wilson  

See the following code I tried:  

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.

Marco vd Heijden
Moderator
8147
Marco vd Heijden  
if(time.hour<10)
 {
  currtime=currtime+"0"+IntegerToString(time.hour)+ ":";
 }
jaffer wilson
747
jaffer wilson  
Marco vd Heijden:

Is the condition I have to apply after my statement. Say for example:  

string currenttime=IntegerToString(time.year)+"-0"+
                      IntegerToString(time.mon)+"-"+
                      IntegerToString(time.day)+ " "+
                      IntegerToString(time.hour)+ ":"+
                      IntegerToString(time.min)+ ":00";
if(time.hour<10)
 {
  currtime=currtime+"0"+IntegerToString(time.hour)+ ":";
 }

Or only the statements you have suggested .please let me know

whroeder1
17949
whroeder1  
jaffer wilson: 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,

Of course leading zeros are omitted. Why would you expect 8 to be converted to 08 or 0000008?

Format it how you want.

string as_string(const MqlDateTime& time){
   return StringFormat("%i-%02i-%02i %02i:%02i:00",
                       time.year, time.mon, time.day, time.hour,time.min);
jaffer wilson
747
jaffer wilson  
whroeder1:

Because you are converting each piece. Why would you expect 8 to be converted to 08 or 0000008?

Format it how you want.

Actually, I am trying to compare the dates from the MQL5 and the test file that I am using to predict stuff. And the file has that format available. While I am comparing the comparison and not making it possibl for me to get teh utput just because of the zero that got added in extra. Can you help?

whroeder1
17949
whroeder1  
jaffer wilson: Actually, I am trying to compare the dates from the MQL5 and the test file

Then do that. Convert the text to a datetime and compare them.

Marco vd Heijden
Moderator
8147
Marco vd Heijden  

Thjere are several way's.

Maybe 

TimeToString();

And it also has flags

TIME_DATE
TIME_MINUTES
TIME_SECONDS

Here: https://www.mql5.com/en/docs/convert/timetostring

string  TimeToString( 
   datetime  value,                           // number 
   int       mode=TIME_DATE|TIME_MINUTES      // output format 
   );
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
Marco vd Heijden
Moderator
8147
Marco vd Heijden  
jaffer wilson:

Is the condition I have to apply after my statement. Say for example:  

Or only the statements you have suggested .please let me know

In between of course so it add the "0" before the hour if <10.

Marco vd Heijden
Moderator
8147
Marco vd Heijden  

11. Date and Time Functions

MQL4 
 MQL5Description
int Day()
int DayMQL4()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.day);
  }
Day
Returns the current day of the month, i.e., the day of month of the last known server time.
TimeCurrent, MqlDateTime
int DayOfWeek()
int DayOfWeekMQL4()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.day_of_week);
  }
DayOfWeek
Returns the current zero-based day of the week (0-Sunday,1,2,3,4,5,6) of the last known server time.
TimeCurrent, MqlDateTime
int DayOfYear()
int DayOfYearMQL4()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.day_of_year);
  }
DayOfYear
Returns the current day of the year (1 means 1 January,..,365(6) does 31 December), i.e., the day of year of the last known server time.
TimeCurrent, MqlDateTime
int Hour()
int HourMQL4()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.hour);
  }
Hour
Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).
TimeCurrent, MqlDateTime
int Minute()
int MinuteMQL4()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.min);
  }
Minute
Returns the current minute (0,1,2,..59) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).
TimeCurrent, MqlDateTime
int Month()
int MonthMQL4()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.mon);
  }
Month
Returns the current month as number (1-January,2,3,4,5,6,7,8,9,10,11,12), i.e., the number of month of the last known server time.
TimeCurrent, MqlDateTime
int Seconds()
int SecondsMQL4()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.sec);
  }
Seconds
Returns the amount of seconds elapsed from the beginning of the current minute of the last known server time by the moment of the program start (this value will not change within the time of the program execution).
TimeCurrent, MqlDateTime
datetime TimeCurrent()
datetime TimeCurrent()
TimeCurrent
Returns the last known server time (time of incoming of the latest quote) as number of seconds elapsed from 00:00 January 1, 1970.
TimeCurrent
int TimeDay(datetime date)
int TimeDayMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.day);
  }
TimeDay
Returns day of month (1 - 31) for the specified date.
TimeToStruct, MqlDateTime
int TimeDayOfWeek(datetime date)
int TimeDayOfWeekMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.day_of_week);
  }
TimeDayOfWeek
Returns the zero-based day of week (0 means Sunday,1,2,3,4,5,6) for the specified date.
TimeToStruct, MqlDateTime
int TimeDayOfYear(datetime date)
int TimeDayOfYearMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.day_of_year);
  }
TimeDayOfYear
Returns day (1 means 1 January,..,365(6) does 31 December) of year for the specified date.
TimeToStruct, MqlDateTime
int TimeHour(datetime time)
int TimeHourMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.hour);
  }
TimeHour
Returns the hour for the specified time.
TimeToStruct, MqlDateTime
datetime TimeLocal()
datetime TimeLocal()
TimeLocal
Returns local computer time as number of seconds elapsed from 00:00 January 1, 1970.
TimeLocal
int TimeMinute(datetime time)
int TimeMinuteMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.min);
  }
TimeMinute
Returns the minute for the specified time.
TimeToStruct, MqlDateTime
int TimeMonth(datetime time)
int TimeMonthMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.mon);
  }
TimeMonth
Returns the month number for the specified time.
TimeToStruct, MqlDateTime
int TimeSeconds(datetime time)
int TimeSecondsMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.sec);
  }
TimeSeconds
Returns the amount of seconds elapsed from the beginning of the minute for the specified time.
TimeToStruct, MqlDateTime
int TimeYear(datetime time)
int TimeYearMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.year);
  }
TimeYear
Returns year for the specified date. The returned value can be within the range of 1970 to 2037.
TimeToStruct, MqlDateTime
int Year()
int YearMQL4()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.year);
  }
Year
Returns the current year, i.e., the year of the last known server time.
TimeCurrent, MqlDateTime
Day - Date and Time - MQL4 Reference
Day - Date and Time - MQL4 Reference
  • docs.mql4.com
Day - Date and Time - MQL4 Reference
nicholi shen
1841
nicholi shen  
whroeder1:

Then do that. Convert the text to a datetime and compare them.

I strongly agree that this is the only option that should be considered. 

Marco vd Heijden
Moderator
8147
Marco vd Heijden  

Please don't get fooled by people telling you that 'there is only one way'.

In the end all that matters is that your code does what you designed it to do, and that it (hopefully) pay's off.

How you got there is less important then getting there.

There are many many ways to make something work.

If someone claims that there is only one way to the top, it shows a very narrow mindset. 

In stead try all things that you can come up with and pick the one that performs best.

The other attempts will come in handy when you try to solve other problems in the future.

12
To add comments, please log in or register