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

# TryEncodeDateWeek

**TryEncodeDateWeek** calculates the **TDateTime** value that represents a specified day of a specified week in a specified year, and 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.

* **AYear** must be between 1 and 9999 inclusive.

* **AWeekOfYear** is the week number within the specified year, where 1 is the first week that has four or more days.

  * If the first calendar day of the year is a Friday, Saturday, or Sunday, then those three days must be expressed using **AYear** set to the previous year, and **AWeekOfYear** set to the number of weeks in the previous year.
  * Similarly, if the last calendar day of the year is a Monday, Tuesday, or Wednesday, then those three days are expressed with **AYear** set to the following year and **AWeekOfYear** set to 1.

* **ADayOfWeek** is the day of the week, where 1 is Monday and 7 is Sunday.

**AValue** returns the specified date as a **TDateTime** value, where the time portion is set to midnight 00:00:00 at the start of the specified day.

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

If any of the specified values are not within range, **TryEncodeDateWeek** returns a boolean value of **False**, and the output parameter **AValue** is set to zero.

**Declaration**: Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; out AValue: TDateTime; const ADayOfWeek: Word): Boolean;

For example.

```pascal
procedure OnMapEvent(var Value:Variant);
var
  aYear, aWeek, aDay : word;
  TheDate, ThatDate : TDateTime;
  aGo: boolean;
begin
 //This first test will pass
  aYear := 2024;
  aWeek := 27;
  aDay := 5;
  TheDate := EncodeDateWeek(aYear, aWeek, aDay)       //Date is 05-05-2024
  LogInfo(FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', TheDate));
  aGo := TryEncodeDateWeek(aYear, aWeek, ThatDate, aDay);
 If aGo = True then
    LogInfo('The date works.')
 else
    LogInfo('The date DOES NOT work.');

 //This next test will fail
  aYear := 2024;
  aWeek := 31;
  aDay := 8;
  aGo := TryEncodeDateWeek(aYear,aWeek, ThatDate, aDay);
 If aGo = True then
    LogInfo('The date works.')
 else
    LogInfo('The date DOES NOT work.');

end;
```
