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

# TryEncodeDateMonthWeek

**TryEncodeDateMonthWeek** calculates the **TDateTime** value that is represented by a specified day for a specified week, in a specified month and year, and tests whether a valid **TDateTime** value would be generated from those specified individual parts.

The time portion of the return **AValue** is 00:00:00 - midnight at the start of the specified day **ADayOfWeek**.

The definitions for **AWeekOfMonth** and **ADayOfWeek** follow the ISO 8601 standard.

* **AYear** is the year which can be any value from 1 through 9999 inclusive.

* **AMonth** can be any month value from 1 through to 12 inclusive.

* **AWeekOfMonth** is the week within **AMonth**, where 1 is the first week that includes four or more days.

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

* **ADayOfWeek** is the day of the week, where 1 is Monday, 2 is Tuesday e.t.c., with 7 representing Sunday.

If any of the specified parameters are not within range, **TryEncodeDateMonthWeek** will return **False**.

**TryEncodeDateMonthWeek** returns **True** if the component parts **AYear**, **AMonth**, **AWeekOfMonth**, and **ADayOfWeek** produce a date that represents a valid **TDateTime** value.

**Declaration**: function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; var AValue: TDateTime): Boolean;

```pascal
procedure OnMapEvent(var Value:Variant);
var
  aYear, aMonth, aWeek, aDay : word;
  yDate, yDate1 : TDateTime;
begin
 //This test will pass
  aYear := 2024;
  aMonth := 10;
  aWeek := 2;
  aDay := 3;
  aGo := TryEncodeDateMonthWeek(aYear, aMonth, aWeek, aDay, yDate);
 If aGo = True then
    LogInfo('The date works - '+FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', yDate))
 else
    LogInfo('The date DOES NOT work - '+FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', yDate));

 //This test will fail
  aYear := 2024;
  aMonth := 10;
  aWeek := 2;
  aDay := 8;
  aGo := TryEncodeDateMonthWeek(aYear, aMonth, aWeek, aDay, yDate1);
 If aGo = True then
    LogInfo('The date works - '+FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', yDate1))
 else
    LogInfo('The date DOES NOT work - '+FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', yDate1));

end;
```
