Black Algo Technologies
Understanding the Datetime Data Type
As you continue to build and test robots, you will eventually come across cases where date and time
manipulation is needed. To do that, you need to understand the datetime data type. We have briefly
looked at date and time in the Chapter “Programming Basics 3”. We will look at it in detail here.
Contents of Datetime
Datetime is just another type of data, just like integers and doubles. The datetime data type is intended
for storing the date and time as the number of seconds elapsed since January 01, 1970.
For instance, if the variable hello1 holds the date time value for 1970 Jan 1st [Link], hello1 will return
60 as 60 seconds has passed since January 01, 1970. If hello1 holds the value for 2015 Aug 16th
[Link], datetime will return 1,439,751,600 as that is the number of seconds that has passed since
January 01, 1970.
Declaration and Initialisation
We can’t possibly be expected to initialise our datetime variables using number of seconds since 1970.
Fortunately for us, MT4 allows us to initialise datetime variables using a D character and string in
quotation marks.
Examples:
datetime NY=D'2015.01.01 00:00'; // Time of beginning of year 2015
datetime d1=D'1980.07.19 [Link]'; // Year Month Day Hours Minutes Seconds
datetime d2=D'19.07.1980 [Link]'; // Equal to D'1980.07.19 [Link]';
datetime d3=D'19.07.1980 12'; // Equal to D'1980.07.19 [Link]'
datetime d4=D'01.01.2004'; // Equal to D'01.01.2004 [Link]'
Read more here: [Link]
Output a Datetime Variable
When we output a datetime variable using Print() and Comment(), there are two types of output:
1) If we include the line #property strict, at the top of the code
Then we will output
[Link]
Black Algo Technologies
2) If we do not include the line #property strict, at the top of the code
Then we will output
#property strict simply tells MetaEditor how to compile the code. See more here:
[Link] (Optional)
By default we keep the line #property strict in all our code.
Date & Time Functions
There are many functions that are related to date and time. These functions are very important for
building robots and you will use them often. These functions can be broadly divided in 2 categories:
1) Functions that return information on the current date & time (Eg. Hour, DayOfWeek)
2) Functions that return information on a specific date & time (Eg. TimeHour, TimeDayOfWeek).
These functions usually start will the word “Time”.
Read more: [Link]
Converting Datetime to String
There are two functions that allows us to convert datetime to string and vice versa.
Datetime to String: [Link]
String to Datetime: [Link]
[Link]