DelphiBasics
EndOfADay
Function
Generate a TDateTime value set to the very end of a day DateUtils unit
1  function EndOfADay ( const Year, Month, Day : Word ) : TDateTime;
2  function EndOfADay ( const Year, DayOfYear : Word ) : TDateTime;
Description
The EndOfADay function generates a TDateTime value set to the given year, month and day with a time set to 1 milli-second before midnight.
 
Version 1
 
Allows the Month and Day to be specified separately. The month must be between 1 (January) and 12 (December). The day must be between 1 and 31, depending on the year and month.
 
Version 2
 
Allows the Month and Day to be specified as a DayOfYear instead.
Notes
WARNING there appears to be a bug in Delphi (as tested by the author using Delphi 7.0 Professional build 4.453), where the long syntax version incorrectly uses the EndOfAMonth routine to calculate the end of the day.

Errors in the parameter values gives EConvertError.
Related commands
EndOfAMonth Generate a TDateTime value set to the very end of a month
 Download this web site as a Windows program.

Development Boards at very low prices with free postage.


 
Example code : Set the date to last msec of the 20th century - illustrates Delphi bug
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  DateUtils,   // Unit containing the EndOfADay command
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  mydate : TDateTime;

begin
  myDate := EndOfADay(1999, 365);

  // Ensure that milli-seconds are shown
  LongTimeFormat := 'hh:mm:ss.zzz';
  ShowMessage('End of 1999 using short syntax = '+DateTimeToStr(myDate));

  myDate := EndOfADay(1999, 12, 31);

  // Ensure that milli-seconds are shown
  LongTimeFormat := 'hh:mm:ss.zzz';
  ShowMessage('End of 1999 using  long syntax = '+DateTimeToStr(myDate));
  ShowMessage('WARNING - The above value is incorrect');
end;
 
end.
Hide full unit code
   End of 1999 using short syntax = 31/12/1999 23:59:59.999
   End of 1999 using  long syntax = 30/01/2000 23:59:59.999
   WARNING - The above value is incorrect
 
 
Delphi Programming Neil Moffatt 2002 - 2014. All rights reserved.  |  Contact the author  |  Home Page