MQL4|DateTime Type (datetime)

※記事内に広告を含む場合があります。

Purpose
The DateTime type (datetime) is used to represent dates and times. This data type is expressed as the total number of seconds since January 1, 1970. DateTime type is very useful for recording specific dates and times in the past or future, and for managing timestamps of transactions.

Size
The size of the DateTime type is 4 bytes. This 4-byte memory area stores the total number of seconds since January 1, 1970.

Value Range
The value range of the DateTime type is from January 1, 1970, 00:00 to December 31, 2037, 23:59. This range can be represented by a 32-bit integer.

Usage Example
Below is an example of using the DateTime type. In this example, the current date and time are obtained, formatted, and output.

void OnStart() {
    // Get the current date and time
    datetime currentTime = TimeCurrent();
    
    // Format the date and time
    string formattedTime = TimeToString(currentTime, TIME_DATE | TIME_MINUTES);
    
    // Output the result
    Print("The current date and time is " + formattedTime + ".");

    // Calculate the date and time one day ago
    datetime oneDayAgo = currentTime - 24 * 60 * 60;
    string formattedOneDayAgo = TimeToString(oneDayAgo, TIME_DATE | TIME_MINUTES);
    Print("The date and time one day ago was " + formattedOneDayAgo + ".");
}

In this code, the DateTime type is used in the following steps:

  1. The TimeCurrent function is used to get the current date and time, which is stored in currentTime.
  2. The TimeToString function is used to format currentTime, and the result is stored as a string in formattedTime. This function converts the date and time into a readable format.
  3. The Print function is used to output the current date and time.
  4. The date and time one day ago is calculated by subtracting 24 hours (1 day) from the current date and time, and stored in oneDayAgo.
  5. Similarly, the TimeToString function is used to format the date and time one day ago, and the result is stored in formattedOneDayAgo.
  6. The Print function is used to output the date and time one day ago.

Thus, the DateTime type is very convenient for managing dates and times within a program. It can be used for handling transaction timestamps or manipulating specific dates and times.

※記事内に広告を含む場合があります。
ベアちゃん@東京シストレ: One of Japan's oldest MetaTrader FX automated trading developers 🔧. With over 15 years of professional experience, he boasts achievements such as winning first place in the Tradency Tournament '15 🥇 and being the runner-up in the 3rd EA-1 Grand Prix 🥈. He has also appeared on Radio Nikkei and is currently active as an executive of Trilogy Corporation (a registered investment advisory and agency business) [Registration number: 372, Director of the Kinki Finance Bureau, Ministry of Finance]. To join the board of Trilogy Corporation, he underwent a rigorous personal examination by the Kinki Finance Bureau, and his name is now registered with the Kinki Finance Bureau. Becoming an executive officer of a registered investment advisory and agency business requires passing this stringent personal examination. Utilizing his extensive knowledge as a former senior design engineer, he provides insights into the development of high-performance Expert Advisors (EAs), the latest trading technologies, and market analysis. He delivers professional strategies and tips to traders aiming to optimize their FX trading and maximize profits.

This website uses cookies.