Skip to main content

DayOfTheYear

Function DayOfTheYear(const AValue: TDateTime): Word

Example

procedure ScriptEvent(var Value: variant);
begin
Value := DayOfTheYear(EncodeDate(2024, 12, 31)); // 366
end;

Usage

DayOfTheYear returns the one-based ordinal day within the calendar year for a TDateTime value.

Parameters

NameTypeDescription
AValueTDateTime, constValue whose calendar date supplies the year and day. Time is ignored by truncation.

Returns

1 through 365, or 366 in a leap year after 28 February.

Behaviour

The result follows Gregorian leap-year rules. It is a calendar ordinal, not a count of elapsed 24-hour periods from the exact time at the start of the year.

Errors

Valid encoded dates do not raise. Invalid decoded years can propagate an EConvertError from EncodeDate in the start-of-year helper.

Additional Technical Info

DayOfTheYear returns the one-based ordinal position of a date within its calendar year. January 1 is 1; the last day is 365 or 366 according to leap-year rules.

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

Implementation

The DateUtils import binds to Delphi 37.0 System.DateUtils.DayOfTheYear. It computes Trunc(AValue) - Trunc(StartOfTheYear(AValue)) + 1. StartOfTheYear decodes the year and encodes 1 January.

Edge cases and quirks

  • The time of day is removed with Trunc, including Delphi's special negative-date convention.
  • Unsupported dates before year 1 can cause the delegated year decode/EncodeDate path to fail rather than return an ordinal.
  • A timezone is not attached or applied. Convert the value first if the day must be determined in a different timezone.
  • Free Pascal exposes the same one-based concept; Velox uses Delphi's exact start-of-year arithmetic.

Side effects

None.

Performance and concurrency

Constant-time calendar decode/encode and subtraction with no allocation or shared state.

Related entries

External references

Created 2026-07-15