DelphiBasics
RecodeTime
Function
Change only the time part of a TDateTime variable DateUtils unit
 function RecodeTime ( const SourceDate : TDateTime; const Hour, Min, Sec, MSec : Word ) : TDateTime;
Description
The RecodeTime function allows the Hour, Min, Sec and MSec (milli-second) values of a TDateTime variable to be changed without affecting the date.
Related commands
DecodeDate Extracts the year, month, day values from a TDateTime var.
DecodeDateTime Breaks a TDateTime variable into its date/time parts
DecodeTime Break a TDateTime value into individual time values
EncodeDate Build a TDateTime value from year, month and day values
EncodeDateTime Build a TDateTime value from day and time values
EncodeTime Build a TDateTime value from hour, min, sec and msec values
RecodeDate Change only the date part of a TDateTime variable
ReplaceDate Change only the date part of a TDateTime variable
ReplaceTime Change only the time part of a TDateTime variable
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : Change the time without changing the date
// 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 RecodeTime 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
  // Set the date to 29/10/2002 at 12:34:56
  myDate := EncodeDateTime(2002, 10, 29, 12, 34, 56, 0);
  ShowMessage('The starting date/time = '+DateTimeToStr(myDate));

  // Now change the time portion without touching the date
  myDate := RecodeTime(myDate, 7, 35, 22, 0);
  ShowMessage('The new      date/time = '+DateTimeToStr(myDate));
end;
 
end.
Hide full unit code
   The starting date/time = 29/10/2002 12:34:56
   The new date/time      = 29/10/2002 07:35:22
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page