Skip to main content

TryRecodeDateTime

Function TryRecodeDateTime(const AValue: TDateTime; const AYear, AMonth,
ADay, AHour, AMinute, ASecond, AMilliSecond: Word;
var AResult: TDateTime): Boolean

Example

procedure ScriptEvent(var Value: variant);
var
InputValue, ResultValue: TDateTime;
begin
InputValue := EncodeDateTime(2026, 7, 18, 9, 30, 15, 250);
if TryRecodeDateTime(InputValue, 65535, 12, 31, 23, 59, 59, 999,
ResultValue) then
Value := ResultValue;
end;

Usage

TryRecodeDateTime tries to replace selected date/time fields while retaining each field marked with the recode sentinel.

Parameters

NameTypeDescription
AValueTDateTime, constBase value decoded into seven fields.
AYearWord, constYear 1..9999, or 65535 to preserve it.
AMonthWord, constMonth 1..12, or 65535.
ADayWord, constValid resolved day, or 65535.
AHourWord, constHour 0..23, or 65535.
AMinuteWord, constMinute 0..59, or 65535.
ASecondWord, constSecond 0..59, or 65535.
AMilliSecondWord, constMillisecond 0..999, or 65535.
AResultTDateTime, varReceives the complete result on success; can receive a date-only partial result on failure.

Returns

The Boolean result of encoding the resolved fields.

Errors

Expected invalid resolved fields return False. Unexpected decoding or numeric faults are not translated.

Additional Technical Info

TryRecodeDateTime decodes a base value, substitutes each supplied field and attempts to encode the resolved combination. Passing 65535 (High(Word)) preserves the corresponding base field. Invalid combinations return False instead of invoking DateUtils' recode error constructor.

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.TryRecodeDateTime. It calls DecodeDateTime, replaces each local field not equal to RecodeLeaveFieldAsIs, then delegates to TryEncodeDateTime, including its sign-aware pre-epoch composition and output behaviour.

Edge cases and quirks

  • The symbolic sentinel is not exposed as a Code Library constant; scripts use numeric 65535 deliberately.
  • Fields are validated as one combination. Preserving day 31 while changing to a 30-day month returns False; nothing is clamped.
  • Exact 24:00:00.000 returns False because the installed terminal rejects hour 24.
  • When the resolved date is valid but resolved time is invalid, delegated TryEncodeDateTime writes the date at midnight to AResult before returning False. Ignore all output after failure.
  • An invalid/out-of-range base can decode zero fields; preservation sentinels then retain those invalid zeros and the final encode fails.
  • No timezone conversion occurs.

Side effects

May write a complete or partial value to AResult; no external state changes.

Performance and concurrency

Constant-time decode, substitution and encode work with no I/O or shared state.

Related entries

External references

Created 2026-07-15