---
url: >-
  /statelake/scripting-reference/functions-and-procedures/datetime-functions/tryencodedatetime.md
---

# TryEncodeDateTime

This function tests whether a valid **TDateTime** value would be generated from the specified individual parts.

There are constraints as to the values that are accepted for the input parameters.

* The year must be between 1 and 9999.
* Valid month values are 1 through 12.
* Valid day values are 1 through 28, 29, 30, or 31, depending on the month value. For example, the possible day values for month 2 (February) are 1 through 28 or 1 through 29, depending on whether or not the year value specifies a leap year.
* Valid hour values are 0 through 24. If the specified hour is 24, the minute, second, and millisecond values should all be 0, and the resulting **TDateTime** value represents midnight at the end of the specified day and the beginning of the next day.
* Valid minute values are 0 through 59.
* Valid second values are 0 through 59.
* Valid millisecond values are 0 through 999.

If the specified values are within range, the **TryEncodeDateTime** function returns a boolean value of **True**, and the encoded date/time value is available from the output parameter **AValue**.

Where the **EncodeDateTime** function will generate an error if any part fails, **TryEncodeDateTime** will not. If any of the specified values are not within range, **TryEncodeDateTime** returns a boolean value of **False**, and the output parameter **AValue** is set to zero. This will display as 30 Dec 1899.

**Declaration**: Function TryEncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; out AValue: TDateTime): Boolean;

For example.

```pascal
procedure OnMapEvent(var Value:Variant);
var
  lYear, lMonth, lDay, lHour, lMin, lSec, lMil : word;
  lDate: TDateTime;

begin
 //Test for valid creation of a date/time from its separate parts
  Value := TryEncodeDateTime(lYear, lMonth, lDay, lHour, lMin, lSec, lMil, lDate);

end;
```

Please also refer to **EncodeDateTime** at [EncodeDateTime](/statelake/scripting-reference/functions-and-procedures/datetime-functions/encodedatetime) and **DecodeDateTime** at [DecodeDateTime](/statelake/scripting-reference/functions-and-procedures/datetime-functions/decodedatetime).
