The function TryEncodeDateDay calculates the TDateTime value that is represented by a specified day for a specified year, and tests whether a valid TDateTime value would be generated from those specified individual parts.

  • AYear is the year that must be between 1 and 9999 inclusive.

  • ADayOfYear is the day within the specified year, where day 1 is 1st January, day 2 is 2nd of January, day 32 is 1st February e.t.c.

If the specified values are within range, the TryEncodeDateDay function returns a boolean value of True, and the encoded TDateTime value is available from the output parameter AValue. The time portion of AValue is set to 00:00:00 midnight at the start of the specified day.

Where a valid date is represented, then TryEncodeDateDay returns a boolean value of True.

A boolean result of False will be returned where ADayOfYear is less than 1, or greater than the number of days available in AYear.

If the result is False, then AValue will be set to zero(0) and will display a date of 30-12-1899.

Declaration: Function TryEncodeDateDay(const AYear, ADayOfYear: Word; out AValue: TDateTime): Boolean;

For example.

procedure OnMapEvent(var Value:Variant);
var
aYear, aDay : word;
FDate, FDate1: TDateTime;
 
begin
//This test will pass
aYear := 2024;
aDay := 366;
aGo := TryEncodeDateDay(aYear, aDay, FDate);
If aGo = True then
LogInfo('The date works - '+FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', FDate))
else
LogInfo('The date DOES NOT work - '+FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', FDate));
 
//This test will fail
aYear := 2022;
aDay := 366;
aGo := TryEncodeDateDay(aYear, aDay, FDate1);
If aGo = True then
LogInfo('The date works - '+FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', FDate1))
else
LogInfo('The date DOES NOT work - '+FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', FDate1));
 
end;