改变今天的日期,推进一个月并设置系统时间

Bru*_*oon 4 delphi datetime

我想要一个函数的代码示例,该函数采用tDateTime和一个整数作为输入,并在将tDateTime推进(int)个月之后使用setlocaltime设置系统时间.时间应该保持不变.

伪代码示例

SetNewTime(NOW,2);
Run Code Online (Sandbox Code Playgroud)

我遇到的问题相当令人沮丧.我不能使用incDth或类似的tDateTime,只能使用tDate等.

Jos*_*ons 5

下面是一个适合我的完整命令行程序.在Delphi 5和2007中测试过.为什么说IncMonth对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.
Run Code Online (Sandbox Code Playgroud)