Undeclared identifier error when using TimeCurrent and TimeHour in MQL5

 
Hi,

I'm having trouble compiling a few simple time-related helper functions in my MQL5 code. The compiler gives me the following errors:

  • 't' - some operator expected

  • 'TimeHour' - undeclared identifier

  • Similar errors for TimeDayOfWeek and TimeDay

Here's an example of the code that's causing the issue:

int ServerHour() { datetime t = TimeCurrent(); return TimeHour(t); } int ServerDOW() { datetime t = TimeCurrent(); return TimeDayOfWeek(t); } int ServerDay() { datetime t = TimeCurrent(); return TimeDay(t); }

To me, the syntax looks correct — I'm calling TimeCurrent() to get the current server time, then using TimeHour() , TimeDayOfWeek() , and TimeDay() to extract components from it. But MetaEditor is not recognizing these functions or the variable t , even though it's clearly declared.

So far, I've tried:

  • Retyping the entire function manually (not copy/pasted)

  • Verifying that I'm working in an .mq5 file (not .mq4 )

  • Restarting MetaEditor and the terminal

Despite these steps, I keep getting the same errors. It's almost as if the editor doesn't recognize built-in functions like TimeCurrent() or TimeHour() . Strangely, TimeCurrent() sometimes appears in purple instead of blue, which suggests it's being interpreted as a string, not a function.

Has anyone encountered this before? Is it a parsing bug, encoding issue, or something else?

Any help would be appreciated!

 
Trading Funded:
You should insert code using the Code button or Alt+S; this time I fixed it myself so it displays correctly.
 
Trading Funded:

I'm having trouble compiling a few simple time-related helper functions in my MQL5 code. The compiler gives me the following errors:

  • 't' - some operator expected

  • 'TimeHour' - undeclared identifier

  • Similar errors for TimeDayOfWeek and TimeDay

Don't request help for A.I. generated code. It generates horrible invalid code.

TimeHour, TimeDayOfWeek, TimeDay are not valid MQL5 functions. They are MQL4.

Research the Documentation, Articles and Codebase, or use the Freelance section for such requests.

 

  • 't' - some operator expected

  • 'TimeHour' - undeclared identifier

  • Similar errors for TimeDayOfWeek and TimeDay


As mentioned above, these errors are typical of AI doing MQL5 - before asking for help from the forum, at least ask the AI first.

Undeclared identifier errors are typical when the AI uses an MQL4 only function. For instance, ask this question "Are you sure that TimeHour is an MQL5 function?"

Nine times out of ten it'll realise the mistake and rewrite the code with the correct MQL5 command.