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
Public property CurrentTime object Gets current time of day only.
Public property Day int Gets and sets the day portion of the object's date.
Public property 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).
Public property ELDateTimeEx double Gets and sets the date and time in legacy EasyLanguage format.
Public property 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).
Public property Hour int Gets and sets the hour portion of the time.
Public property Minute int Gets and sets the minute portion of the object's time.
Public property Month int Gets and sets the month portion of the object's date.
Public property Now object Gets the current date and time of object's day.
Public property Second int Gets and sets the seconds portion of the object's time.
Public property TimeOfDay object Gets the time of day (as a TimeSpan object).
Public property Today object Gets the current date only.
Public property Value 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.
Public property Year int Gets and sets the year portion of the object's date.
Methods
  Name Description
Public property AddDays(nDays) Adds the specified number of days to the DateTime value.
Public property AddHours(nHours) Adds the specified number of hours to the DateTime value.
Public property AddMinutes(nMinutes) Adds the specified number of minutes to the DateTime value.
Public property AddMonths(nMonths) Adds the specified number of months to the DateTime value.
Public property AddSeconds(nSeconds) Adds the specified number of seconds to the DateTime value.
Public property AddWeeks(nWeeks) Adds the specified number of weeks to the DateTime value.
Public property AddYears(nYears) Adds the specified number of years to the DateTime value.
Public property Create Initializes an instance of the DateTime class.
Public property Create(y,m,d,h,m,s) Initializes an instance of DateTime with the specified year, month, day, hour, minute, and seconds parameters.
Public property Create(y,m,d) Initializes an instance of DateTime with the specified year, month, and day.
Public property Create(eldate) Initializes an instance of the DateTime class using an ELDate.
Public property ELFormatDate(format) Converts the DateTime value to a string using EL format codes "ddd MMM dd yy" (see FormatDate reserved word).
Public property ELFormatTime(format) Converts the DateTime value to a string using EL format codes "hh:mm:ss tt" (see FormatTime reserved word).
Public property Equals(obj) True if Obj is equal to the current instance, otherwise False.
Public property Format(format) Converts the DateTime value to a string using % format codes (see below).
Public property FromELDateAndTime(ElDate,ELTime) Converts legacy EL date and EL time values to a DateTime equivalent.
Public property FromELDateAndTime(ElDate,ELTime,seconds) Converts legacy EL datem, EL time, and Seconds values to a DateTime equivalent.
Public property Parse(string) Converts the date and time string parameter to a DateTime equivalent.
Public property SetToCurrentTime() Sets the DateTime value to the current date and time.
Public property ToString() Converts the DateTime value to a string.
Public property TryParse(string,result) True if date and time string will convert successfully to the result DateTime.
Operators
  Name Description
Public property operator+ Adds a specified TimeSpan to a specified DateTime, yielding a new date and time.
Public property operator+= Adds a specified TimeSpan to the current DateTime instance, yielding a new date and time.
Public property operator- Subtracts a specified DateTime from a specified TimeSpan, yielding a new TimeSpan.
Public property operator- Subtracts a specified TimeSpan from a specified DateTime, yielding a new date and time.
Public property operator-= Subtracts a specified TimeSpan from the current DateTime instance, yielding a new date and time.
Public property operator< True when one specified DateTime is less than another specified DateTime.
Public property operator<= True when one specified DateTime is equal to or less than another specified DateTime.
Public property operator> True when one specified DateTime is greater than another specified DateTime.
Public property 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