+1 888 340 6572

Outputting Dates in EasyLanguage

From MultiCharts

This article gives some tips and examples for outputting dates in EasyLanguage.

Relevant Easylanguage Date Reserved Words

Just as a reminder, the following Date reserved words are often used in working with dates:

  • Date, which returns the Date of the current bar in YYYMMdd format. This date format is always the format with which a date calculation starts.
  • CurrentDate, returns the current computer date in YYYMMdd format.
  • ELDateToDateTime, converts a date in YYmmdd format to a date into DateTime format.


See the Date & Time Routines for all EasyLanguage reserved words.

Outputting the Date

The following example prints the current date to the PowerLanguage Editor log:

Print("The current date is: ", CurrentDate);

Which returns the following sentence: The current date is: 1120127.00.

To output the date in a more readable format, the reserved word FormatDate is needed. However, FormatDate requires that the date is in DateTime format, while the default Date is in YYYMMdd format. So, a conversion to DateTime is needed first. This is done with the ELDateToDateTime reserved word.

The code example below shows in detail which steps needs to be made.

Variables:
	dateToday(0), dateInDateTimeFormat(0), dateReadable("");

// 1. Assign the date of today to the variable 'dateToday'
dateToday = CurrentDate;
Print("The current date in YYYMMdd format is: ", dateToday, NewLine);

// 2. Now, convert the date to DateTime format
dateInDateTimeFormat = ELDateToDateTime(dateToday);
Print("The current date in DateTime format is: ", NumToStr(dateInDateTimeFormat, 5), NewLine);

// 3. Now we can print the current date in a more readable format
dateReadable = FormatDate("dd-MM-yyyy", dateInDateTimeFormat);
Print("The current date in a readable format is: ", dateReadable);

This returns the following:

The current date in YYYMMdd format is: 1120127.00

The current date in DateTime format is: 40935.00000

The current date in a readable format is: 27-01-2012

Of course, not all these steps are needed. The extensive above example can be rewritten to simply:

Print("The current date in a readable format is: ", FormatDate("dd-MM-yyyy", ELDateToDateTime(CurrentDate)));

Which returns:

The current date in a readable format is: 27-01-2012

Outputting the Date in Different Formats

The FormatDate reserved word has a parameter string by which different date outputs can be generated. For example, in the code above we used "dd-MM-yyyy" to return "27-1-2012".

The code example below shows some of these parameter options for FormatDate (see the FormatDate article for an extensive list of parameter options).

Variables:
	dateInDateTime(0);
	
dateInDateTime = ELDateToDateTime(CurrentDate);

Print("Date in short version: ", FormatDate("d-M-y", dateInDateTime), NewLine);

// Note: the output of the following statement depends on your regionale
Print("Date in abbreviations: ", FormatDate("ddd-MMM-yy", dateInDateTime), NewLine);

Print("Date fully written: ", FormatDate("dddd MMMM yyyy", dateInDateTime), NewLine);

Print("Date in a sentence: ", 
	FormatDate("The current Date is dddd anD the month is MMMM (which is month #MM). The Year is yyyy.", dateInDateTime));

end;

This gives the following output:

Date in short version: 27-1-12

Date in abbreviations: vr-jan-12

Date fully written: vrijdag januari 2012

Date in a sentence: The current Date is vrijdag anD the month is januari (which is month #01). The Year is 2012.