Change only the date part of a TDateTime variable DateUtils unit
 function RecodeDate(const SourceDate TDateTime; const Year, Month, Day Word):TDateTime;
The RecodeDate function allows the Year, Month and Day values of a TDateTime variable to be changed without affecting the time.
Related commands
DecodeDateExtracts the year, month, day values from a TDateTime var.
DecodeDateTimeBreaks a TDateTime variable into its date/time parts
DecodeTimeBreak a TDateTime value into individual time values
EncodeDateBuild a TDateTime value from year, month and day values
EncodeDateTimeBuild a TDateTime value from day and time values
EncodeTimeBuild a TDateTime value from hour, min, sec and msec values
RecodeTimeChange only the time part of a TDateTime variable
ReplaceDateChange only the date part of a TDateTime variable
ReplaceTimeChange only the time part of a TDateTime variable
 Download this web site as a Windows program.

Example code : Change the date without changing the time
  myDate : TDateTime;

  // 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 date portion without touching the time
  myDate := RecodeDate(myDate, 1957, 2, 18);
  ShowMessage('The new      date/time = '+DateTimeToStr(myDate));
Show full unit code
  The starting date/time = 29/10/2002 12:34:56
  The new date/time = 18/02/1957 12:34:56
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page