How do I get the timestamp of 'NOW()', i.e. what is the TIME this instant upon execution of this line of code?

 

I want to be able to print the timestamp something like this:

Print("The time this instant is "+FormatString("%Y-%M-%D %h:%mm:%ss.%uuu",NOW()));

To be clear, I want to print the exact time stamp of when this Print function is run, for debugging purposes. I want to insert this into my code at different points so I can understand when something was run.


Thanks.

 
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Date Type Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Date Type Structure
  • www.mql5.com
Date Type Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Steve:

I want to be able to print the timestamp something like this:

Print("The time this instant is "+FormatString("%Y-%M-%D %h:%mm:%ss.%uuu",NOW()));
  1. Do you actually have to have that exact format? You are making a lot of work for yourself.
    1. You have to convert NOW() to its parts as Lippmaje says. Or convert it to a string and then select the appropriate parts
    2. Then format it to your output format.

  2. Or just use
    TimeToString(NOW(), TIME_DATE|TIME_MINUTES|TIME_SECONDS)
    and get "yyyy.mm.dd hh:mi:ss" format.

  3. Either case datetimes have no fraction of a second, you can't get that.
 
Thanks. I also discovered this works:
TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);
 

TimeCurrent() returns the time of the last tick. So all logs within the same OnTick will have the same value. Use GetMicrosecondCount() instead.

 
William Roeder:
  1. Do you actually have to have that exact format? You are making a lot of work for yourself.
    1. You have to convert NOW() to its parts as Lippmaje says. Or convert it to a string and then select the appropriate parts
    2. Then format it to your output format.

  2. Or just use and get "yyyy.mm.dd hh:mi:ss" format.

  3. Either case datetimes have no fraction of a second, you can't get that.

Thanks. It's just for debugging, and also learning. "NOW()" is just what it's called in Excel. I'm getting my head around how and when things are executed in MQL.

 
Steve:

Thanks. It's just for debugging, and also learning. "NOW()" is just what it's called in Excel. I'm getting my head around how and when things are executed in MQL.

NOW() is not known in MQL. It's either server time, TimeCurrent, or local time, TimeLocal. Both are measured in seconds. Beyond that, MT5 offers milliseceonds for server time, see MqlTick. GetMicrosecondCount and GetTickCount are not synchronized to the second wrap.
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Price Data Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Price Data Structure
  • www.mql5.com
This is a structure for storing the latest prices of the symbol. It is designed for fast retrieval of the most requested information about current prices. The parameters of each tick are filled in regardless of whether there are changes compared to the previous tick. Thus, it is possible to find out a...
Reason: