DateTime Class
A class used to represent a point in time. The point in time is represented through properties that represent the calendar date and time-of-day.
Example 1
The following calculates and displays a new DateTime that is 15 minutes greater than the current computer clock time:
using elsystem;
var: DateTime newTime(null);
newTime = DateTime.Now;
newTime.AddMinutes(15);
print("Current Time: ",DateTime.Now.tostring()," New Time: ",newTime.tostring());
Example 2
This example uses the Format method to display the date portion of the current DateTime in an alternate manner:
using elsystem;
print(Datetime.Now.Format(Format("%a %b %d, %Y")); // Wed May 05, 2010
Remarks
If the day of the month is too large for the specified month then the day will be converted automatically to the appropriate day of the next month. The month and year will be incremented as necessary.
Thus, for example, an attempt to set the day value to 30 when the month value is 2 will cause the month to be incremented automatically to 3, and the day to be assigned the value 2 (or 1 if the year value is a leap year).
The example code below illustrates the automatic adjustments made to the month and day when the day value assigned is incompatible with the month value assigned:
using elsystem;
variables:
DateTime DTForTests( NULL );
once
begin
DTForTests = new DateTime;
DTForTests.Year = 2021;
DTForTests.Day = 30;
DTForTests.Month = 2;
// Prints 02/30/2021 becomes 03/02/2021
Print( "02/30/2021 becomes ", DTForTests.Format( "%m/%d/%Y" ) );
DTForTests.Year = 2020;
DTForTests.Day = 30;
DTForTests.Month = 2;
// Prints 02/30/2020 becomes 03/01/2020
Print( "02/30/2020 (a leap year) becomes ", DTForTests.Format( "%m/%d/%Y" ) );
end;
Namespace: elsystem
Properties
Additional properties, methods, and events are described in the classes listed under Inheritance Hierarchy (see below).
Name | Type | Description | |
CurrentTime | object | Gets current time of day only. | |
Day | int | Gets and sets the day portion of the object's date. | |
ELDate | int | Gets and sets the date in legacy EasyLanguage format (YYYMMDD, where YYY is years since 1900, MM is the month, and DD is the day of the month). | |
ELDateTimeEx | double | Gets and sets the date and time in legacy EasyLanguage format. | |
ELTime | int | Gets and sets the time in legacy EasyLanguage format (HHMM, where HH is the hours since midnight and MM is the minutes within the hour). | |
Hour | int | Gets and sets the hour portion of the time. | |
Minute | int | Gets and sets the minute portion of the object's time. | |
Month | int | Gets and sets the month portion of the object's date. | |
Now | object | Gets the current date and time of object's day. | |
Second | int | Gets and sets the seconds portion of the object's time. | |
TimeOfDay | object | Gets the time of day (as a TimeSpan object). | |
Today | object | Gets the current date only. | |
|
string | Gets and sets the current date and time as a string. Equivalent to ToString() when getting a date/time and Parse() when setting a date/time. | |
Year | int | Gets and sets the year portion of the object's date. |
Methods
Name | Description | |
AddDays(nDays) | Adds the specified number of days to the DateTime value. | |
AddHours(nHours) | Adds the specified number of hours to the DateTime value. | |
AddMinutes(nMinutes) | Adds the specified number of minutes to the DateTime value. | |
AddMonths(nMonths) | Adds the specified number of months to the DateTime value. | |
AddSeconds(nSeconds) | Adds the specified number of seconds to the DateTime value. | |
AddWeeks(nWeeks) | Adds the specified number of weeks to the DateTime value. | |
AddYears(nYears) | Adds the specified number of years to the DateTime value. | |
Create | Initializes an instance of the DateTime class. | |
Create(y,m,d,h,m,s) | Initializes an instance of DateTime with the specified year, month, day, hour, minute, and seconds parameters. | |
Create(y,m,d) | Initializes an instance of DateTime with the specified year, month, and day. | |
Create(eldate) | Initializes an instance of the DateTime class using an ELDate. | |
ELFormatDate(format) | Converts the DateTime value to a string using EL format codes "ddd MMM dd yy" (see FormatDate reserved word). | |
ELFormatTime(format) | Converts the DateTime value to a string using EL format codes "hh:mm:ss tt" (see FormatTime reserved word). | |
Equals(obj) | True if Obj is equal to the current instance, otherwise False. | |
Format(format) | Converts the DateTime value to a string using % format codes (see below). | |
FromELDateAndTime(ElDate,ELTime) | Converts legacy EL date and EL time values to a DateTime equivalent. | |
FromELDateAndTime(ElDate,ELTime,seconds) | Converts legacy EL datem, EL time, and Seconds values to a DateTime equivalent. | |
Parse(string) | Converts the date and time string parameter to a DateTime equivalent. | |
SetToCurrentTime() | Sets the DateTime value to the current date and time. | |
ToString() | Converts the DateTime value to a string. | |
TryParse(string,result) | True if date and time string will convert successfully to the result DateTime. |
Operators
Name | Description | |
operator+ | Adds a specified TimeSpan to a specified DateTime, yielding a new date and time. | |
operator+= | Adds a specified TimeSpan to the current DateTime instance, yielding a new date and time. | |
operator- | Subtracts a specified DateTime from a specified TimeSpan, yielding a new TimeSpan. | |
operator- | Subtracts a specified TimeSpan from a specified DateTime, yielding a new date and time. | |
operator-= | Subtracts a specified TimeSpan from the current DateTime instance, yielding a new date and time. | |
operator< | True when one specified DateTime is less than another specified DateTime. | |
operator<= | True when one specified DateTime is equal to or less than another specified DateTime. | |
operator> | True when one specified DateTime is greater than another specified DateTime. | |
operator>= | True when one specified DateTime is greater than or equal to another specified DateTime. |
Format Codes (used with the Format method)
The argument of the Format method consists of string including formatting codes that are preceded by a percent sign (%). Characters that do not begin with % are copied unchanged to destination string.
For example, to plot a formatted date using the abbreviated weekday, name of month, date, and full year:
Plot1(elsystem.Datetime.Today.Format("%a %b %d, %Y")); // Wed May 05, 2010
or to plot the 12-hour time with AM/PM indication:
Plot2(elsystem.Datetime.CurrentTime.Format("%I:%M:%S, %p")); // 01:22:14 PM
or to plot a formatted date and time:
Plot3(elsystem.Datetime.Now.Format("%m-%d-%y %H:%M")); // 05-05-10 13:22
The formatting codes are listed below:
Code | Description |
%a | Abbreviated weekday name |
%A | Full weekday name |
%b | Abbreviated month name |
%B | Full month name |
%c | Date and time representation appropriate for locale |
%d | Day of month as decimal number (01 - 31) |
%H | Hour in 24-hour format (00 - 23) |
%I | Hour in 12-hour format (01 - 12) |
%j | Day of year as decimal number (001 - 366) |
%m | Month as decimal number (01 - 12) |
%M | Minute as decimal number (00 - 59) |
%p | Current locale's A.M./P.M. indicator for 12-hour clock |
%S | Second as decimal number (00 - 59) |
%U | Week of year as decimal number, with Sunday as first day of week (00 - 53) |
%w | Weekday as decimal number (0 - 6; Sunday is 0) |
%W | Week of year as decimal number, with Monday as first day of week (00 - 53) |
%x | Date representation for current locale |
%X | Time representation for current locale |
%y | Year without century, as decimal number (00 - 99) |
%Y | Year with century, as decimal number |
%z, %Z | Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown |
%% | Percent sign |
Inheritance Hierarchy
elsystem.DateTime