mq4 using Day() Month() Year()

 

Hi,

I'm trying to use these functions. For example this one:

https://docs.mql4.com/dateandtime/day

// This will take broker time and turn it into format DD.MM.YYY
string GetTime()
{
   int nday = Day();
   int nmonth = Month();
   int nyear = Year();
   string sdate = IntegerToString(nday) + "." + IntegerToString(nmonth) + "." + IntegerToString(nyear);
   return sdate;    
}

But somehow when this runs I get 0.1.1900 every time. Not the real broker time. What am I doing wrong?

Thanks

Day - Date and Time - MQL4 Reference
Day - Date and Time - MQL4 Reference
  • docs.mql4.com
Day - Date and Time - MQL4 Reference
 

Please, always use the CODE button (Alt-S) when inserting code.

Code button in editor

 

Hello you can maybe use this:

MqlDateTime

The date type structure contains eight fields of the int type:

struct MqlDateTime
  {
   int year;           // Year
   int mon;            // Month
   int day;            // Day
   int hour;           // Hour
   int min;            // Minutes
   int sec;            // Seconds
   int day_of_week;    // Day of week (0-Sunday, 1-Monday, ... ,6-Saturday)
   int day_of_year;    // Day number of the year (January 1st is assigned the number value of zero)
  };

Note

The day number of the year day_of_year for the leap year, since March, will differ from a number of the corresponding day for a non-leap year.

Example:

void OnStart()
  {
//---
   datetime date1=D'2008.03.01';
   datetime date2=D'2009.03.01';
 
   MqlDateTime str1,str2;
   TimeToStruct(date1,str1);
   TimeToStruct(date2,str2);
   printf("%02d.%02d.%4d, day of year = %d",str1.day,str1.mon,
          str1.year,str1.day_of_year);
   printf("%02d.%02d.%4d, day of year = %d",str2.day,str2.mon,
          str2.year,str2.day_of_year);
  }


/*  Result:
   01.03.2008, day of year = 60
   01.03.2009, day of year = 59
*/

See also

TimeToStruct, Structures and Classes

Char, Short, Int and Long Types - Integer Types - Data Types - Language Basics - MQL4 Reference
Char, Short, Int and Long Types - Integer Types - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Char, Short, Int and Long Types - Integer Types - Data Types - Language Basics - MQL4 Reference
 
  1. void OnStart(){ 
    Print(GetTime()); //  24.5.2023
    }

    Works fine for me. You are doing something else.

    credetarud: But somehow when this runs I get 0.1.1900 every time. 

    Probably not connected to a broker.

  2. You want the leading zeros. Simplify, generalize.
    string GetTime(datetime when=0)
    {
       if( when==0) when=TimeCurrent();
       return StringFormat("%02d.%02d.%04d", TimeDay(when), TimeMonth(when), TimeYear(when) );
    }
    void OnStart(){ 
    Print(GetTime()); //   24.05.2023
    }