datetime data type is a special MQL4 data type, which holds a date and time data. You set the datetime variable by using the keyword (D) followed by two signal quotations ('). Between the two signal quotations you write a character line consisting of 6 parts for value of year, month, date, hour, minutes, and seconds. datetime constant can vary from Jan 1, 1970 to Dec 31, 2037.
For example: D'2004.01.01 00:00' // New Year D'1980.07.19 12:30:27' D'19.07.1980 12:30:27' D'19.07.1980 12' //equal to D'1980.07.19 12:00:00' D'01.01.2004' //equal to D'01.01.2004 00:00:00'
We use the keyword datetime to create a datetime variable. For example:
datetime dtMyBirthDay= D'1972.10.19 12:00:00'; datetime dt1= D'2005.10.22 04:30:00';
Now, let's give the date and time functions a study trip:
CurTime:
Syntax:
datetime CurTime( )
The CurTime function returns the current server time, it's not always the exact current server time but it's the last know server time that the terminal has been retrieved it from the server with the last price
quotation.
The return value of the CurTime function is a datetime data type and it's the number of seconds elapsed from 00:00 January 1, 1970.
What if you used this function (and all the date & time functions) in the testing mode - Back Testing? In the testing mode the server time will be modeled by the tester.
Parameters:
This function takes no parameters and return datetime value. Example: if(CurTime()-OrderOpenTime()<360) return(0);
Day:
Syntax: int Day( ) Description:The Day function returns the current day of the month (1, 2, 3, 4, ... 31) of the last known server time. The day is modeled in the testing mode.
Parameters:
This function takes no parameters and return integer value. Example: if(Day()<5) return(0);
DayOfWeek:
Syntax: int DayOfWeek( ) Description:0 = Sunday 1 = Monday 2 = Tuesday 3 = Wednesday 4 = Thursday 5 = Friday 6 = Saturday
The day of week is modeled in the testing mode. Parameters:
This function takes no parameters and return integer value. Example:
// does not work on holidays.
if(DayOfWeek()==0 || DayOfWeek()==6) return(0);
DayOfYear:
Syntax:
int DayOfYear( )
Description:
The DayOfYear function returns the current day of the year (1, 2, 3, .... 365 (366) ) of the last know server time.
The day of year is modeled in the testing mode. Parameters:
This function takes no parameters and return integer value. Example: if(DayOfYear()==245) return(true);
Hour:
Syntax: int Hour( ) Description:The Hour function returns the current hour (0, 1, 2, .... 23 ) of the last know server time. The hour is modeled in the testing mode.
Note: The hour returned by the Hour function is the hour of the moment the program start and will not change during the execution of the program.
Parameters:
This function takes no parameters and return integer value. Example:
if(Hour()>=12 || Hour()<17) return(true);
LocalTime:
Syntax:
datetime LocalTime( )
Description:
The LocalTime function returns the current local computer time. The return value of the LocalTime function is a datetime data type and it's the number of seconds elapsed from 00:00 January 1, 1970. The local time is modeled in the testing mode as well as the server time.
Parameters:
This function takes no parameters and return datetime value. Example: if(LocalTime()-OrderOpenTime()<360) return(0);
Minute:
Syntax: int Minute( ) Description:The Minute function returns the current minute (0, 1, 2, .... 59 ) of the last know server time. Like the Hour function the minute returned by the Minute function is the minute of the moment the program start and will not change during the execution of the program.
Parameters:
This function takes no parameters and return integer value. Example:
if(Minute()<=15) return("first quarter");
Month:
Syntax:
int Month( )
Description:
The Month function returns the current month (0, 1, 2, 3, .... 12 ) of the last know server time. The month is modeled in the testing mode.
Parameters:
This function takes no parameters and return integer value. Example:
if(Month()<=5) return("the first half year");
Seconds:
Syntax:
int Seconds( )
Description:
The Seconds function returns the current seconds (0, 1, 2, .... 59 ) of the minute of the last know server time. Like the Hour and the Minute functions the seconds returned by the Seconds function is the second of the moment the program start and will not change during the execution of the program.
The minute is modeled in the testing mode. Parameters:
This function takes no parameters and return integer value. Example:
if(Seconds()<=15) return(0);
Year:
Syntax:
int Year( )
Description:
The Year function returns the current year of the last know server time. The year is modeled in the testing mode.
Parameters:
This function takes no parameters and return integer value. Example:
// return if the date is within the range from 1 Jan. to 30 Apr., 2006. if(Year()==2006 && Month()<5) return(0);
TimeDay:
Syntax:
int TimeDay(datetime date)
Description:
The TimeDay function returns the day of the month (1, 2, 3, ... 31) of the given date. It extracts the day of the month from the given date
Parameters: datetime date:
The date (datetime data type) you want to extract the day of the month from. Example:
int day=TimeDay(D'2003.12.31'); // day is 31
Syntax:
int TimeDayOfWeek(datetime date)
Description:
The TimeDayOfWeek function returns the day of the week (0,1, 2, .... 6) of the given date. It extracts the day of the week from the given date
Parameters: datetime date:
The date you want to extract the day of the week from. Example:
int weekday=TimeDayOfWeek(D'2004.11.2'); // day is 2 - Tuesday
TimeDayOfYear:
Syntax:
int TimeDayOfYear(datetime date)
Description:
The TimeDayOfYear function returns the day of the year (1, 2, 3, .... 365 (366)) of the given date. It extracts the day of the year from the given date
Parameters: datetime date:
The date you want to extract the day of the year from. Example:
int day=TimeDayOfYear(CurTime());
TimeHour:
int TimeHour(datetime date)
Description:
The TimeHour function returns the hour of the day (0,1, 2, .... 23) of the given date. It extracts the hours of the day from the given date
Parameters: datetime date:
The date you want to extract the hour of the day from. Example:
int h=TimeHour(CurTime());
TimeMinute:
Syntax:
int TimeMinute(datetime date)
Description:
The TimeMinute function returns the minute of the hour (0,1, 2, .... 59) of the given date. It extracts the minute of the hour from the given date
Parameters: datetime date:
The date you want to extract the minute of the hour from. Example:
int m=TimeMinute(CurTime());
TimeMonth:
Syntax:
int TimeMonth(datetime date)
Description:
month of the year from the given date Parameters:
datetime date:
The date you want to extract the month of the year from. Example:
int m=TimeMonth(CurTime());
TimeSeconds:
Syntax:
int TimeSeconds(datetime date)
Description:
The TimeSeconds function returns the seconds of the minute (0,1, 2, .... 59) of the given date. It extracts the seconds of the minute from the given date
Parameters: datetime date:
The date you want to extract the seconds of the minute from. Example:
if(Seconds()<=15) return(0);
TimeYear:
Syntax:
int TimeYear(datetime date)
Description:
The TimeYear function returns the year of the given date. It extracts the year from the given date Parameters:
datetime date:
Example:
// return if the date is within the range from 1 Jan. to 30 Apr., 2006. if(Year()==2006 && Month()<5) return(0);
Hope you enjoyed the lesson! Coders Guru