Skip to main content

TryEncodeDateMonthWeek

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

Example

procedure ScriptEvent(var Value: variant);
var
EncodedDate: TDateTime;
begin
if TryEncodeDateMonthWeek(2026, 7, 1, 1, EncodedDate) then
Value := EncodedDate; // Monday 29 June 2026
end;

Usage

TryEncodeDateMonthWeek tries to construct an ISO weekday from a supplied calendar month and week-of-month number.

Parameters

NameTypeDescription
AYearWord, constOwning calendar year 1..9999.
AMonthWord, constOwning month 1..12.
AWeekOfMonthWord, constISO month-week number 1..5.
ADayOfWeekWord, constMonday 1 through Sunday 7.
AValueTDateTime, varReceives the calculated midnight on success.

Returns

True when the four inputs pass DateUtils' broad range checks; otherwise False.

Errors

Expected range failures return False. Velox does not raise for those failures.

Additional Technical Info

TryEncodeDateMonthWeek converts an ISO-style month-week year, owning month, week number and Monday-based weekday to midnight. Week 1 is the week containing at least four days of the named month, so a valid result can lie in an adjacent calendar month.

The example is fictional, deterministic and source-reviewed. It was not executed by the documentation workflow.

Implementation

The DateUtils import binds directly to Delphi 37.0 System.DateUtils.TryEncodeDateMonthWeek. It validates the broad year/month/week/weekday ranges, encodes the first of the month, gets its ISO weekday, calculates a week/day offset and adjusts for the ISO four-day ownership rule.

Edge cases and quirks

  • This is not the nth literal weekday occurrence. Use TryEncodeDayOfWeekInMonth for that model.
  • Week 1 can begin in the previous calendar month. Late days in week 5 can fall in the following month.
  • Validation proves only AYear, AMonth, week 1..5 and weekday 1..7; it does not require the calculated date to remain in AMonth or prove a concrete fifth occurrence inside it.
  • AValue is not assigned when the broad validation fails. Always use the Boolean result.
  • The script exposes all arguments and has no default weekday.

Side effects

Writes AValue on success only. No shared state or I/O.

Performance and concurrency

Constant bounded calendar arithmetic with no allocation or shared state.

Related entries

External references

Created 2026-07-15