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:
- The
TimeCurrent
function is used to get the current date and time, which is stored incurrentTime
. - The
TimeToString
function is used to formatcurrentTime
, and the result is stored as a string informattedTime
. This function converts the date and time into a readable format. - The
Print
function is used to output the current date and time. - 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
. - Similarly, the
TimeToString
function is used to format the date and time one day ago, and the result is stored informattedOneDayAgo
. - 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.