How to Use Time Functions in MQL4

Understanding and manipulating time is essential in algorithmic trading. MQL4 provides a range of time functions that help traders access and work with time data. This guide will introduce you to some of the key time functions in MQL4.

1. TimeCurrent()

This function returns the last known server time, often used to retrieve the current time.

datetime CurrentTime = TimeCurrent();

2. TimeDay()

Returns the day of the month (from 1 to 31) for the specified date.

int Day = TimeDay(TimeCurrent());

3. TimeDayOfWeek()

Returns the day of the week (from 0 to 6) for the specified date, where Sunday is 0.

int DayOfWeek = TimeDayOfWeek(TimeCurrent());

4. TimeHour()

Returns the hour (from 0 to 23) of the specified time.

int CurrentHour = TimeHour(TimeCurrent());

5. TimeMinute()

Returns the minute (from 0 to 59) of the specified time.

int CurrentMinute = TimeMinute(TimeCurrent());

6. TimeSeconds()

Returns the seconds (from 0 to 59) of the specified time.

int CurrentSeconds = TimeSeconds(TimeCurrent());

7. TimeMonth()

Returns the month (from 1 to 12) of the specified date.

int CurrentMonth = TimeMonth(TimeCurrent());

8. TimeYear()

Returns the year of the specified date.

int CurrentYear = TimeYear(TimeCurrent());

9. TimeDaylightSavings()

Checks if daylight savings time is applied for the server time. Returns 1 if daylight savings time is applied and 0 otherwise.

int DST = TimeDaylightSavings();

10. TimeGMTOffset()

Returns the current difference between GMT time and the local time of the computer where the program is running, in seconds.

int Offset = TimeGMTOffset();


Time functions in MQL4 are essential for ensuring your trading algorithms work at the desired intervals and under the appropriate conditions. Familiarizing yourself with these functions is crucial when developing and refining trading strategies in MQL4.

Click here to return to MQL4 Basics Directory