Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change today's date, advancing one month and setting the systemtime

I would like a code sample for a function that takes a tDateTime and an integer as input and sets the system time using setlocaltime after advancing that tDateTime by (int) months. The time should stay the same.

pseudo code example

SetNewTime(NOW,2);

The issues I'm running into are rather frustrating. I cannot use incmonth or similar with a tDateTime, only a tDate, etc.

like image 376
Bruce the Hoon Avatar asked Oct 07 '08 19:10

Bruce the Hoon


2 Answers

Below is a complete command-line program that works for me. Tested in Delphi 5 and 2007. Why do you say IncMonth does not work for TDateTime?

program OneMonth;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Windows,
  Messages;

procedure SetLocalSystemTime(settotime: TDateTime);
var
  SystemTime : TSystemTime;
begin
  DateTimeToSystemTime(settotime,SystemTime);
  SetLocalTime(SystemTime);
  //tell windows that the time changed
  PostMessage(HWND_BROADCAST,WM_TIMECHANGE,0,0);
end;

begin
  try
    SetLocalSystemTime(IncMonth(Now,1));
  except on E:Exception do
    Writeln(E.Classname, ': ', E.Message);
  end;
end.
like image 64
JosephStyons Avatar answered Oct 17 '22 12:10

JosephStyons


IncMonth should work with a TDateTime:

function IncMonth ( const StartDate  : TDateTime {; NumberOfMonths  : Integer = 1} ) : TDateTime;

Keep in mind a TDate is really just a TDateTime that by convention your ignore the fraction on.

like image 3
Jim McKeeth Avatar answered Oct 17 '22 13:10

Jim McKeeth