DelphiBasics
IncMonth
Function
Increments a TDateTime variable by a number of months SysUtils unit
 function IncMonth(const StartDate TDateTime {; NumberOfMonths Integer = 1}):TDateTime;
Description
The IncMonth function returns a TDateTime value that is NumberOfMonths greater than the passed StartDate value.
 
The time component of the StartDate value is passed unchanged to the output value.
 
The year value is incremented as appropriate.
 
The increment value is optional, being 1 by default.
 
After incrementing the month, if the day value is too high for that month/year, then it is reduced to the highest value for that month/year.
Notes
There is no DecMonth function.

Instead, use IncMonth with a negative increment.
Related commands
IncDayIncrements a TDateTime variable by + or - number of days
IncMinuteIncrements a TDateTime variable by + or - number of minutes
IncYearIncrements a TDateTime variable by a number of years
IncSecondIncrements a TDateTime variable by + or - number of seconds
IncMillisecondIncrements a TDateTime variable by + or - number of milliseconds
 Download this web site as a Windows program.




 
Example code : Add values to an example date
var
  myDate : TDateTime;
begin
  myDate := StrToDate('31/01/2000');  // End of Jan in a leap year
  ShowMessage('myDate = '+DateToStr(myDate));

  // Increment by 1 (default)
  // 31st Jan 2000 ==> 31st Feb 2000 (illegal) ==> 29th Feb 2000
  myDate := IncMonth(myDate);
  ShowMessage('myDate + 1 month = '+DateToStr(myDate));

  // Increment by 12 months
  // 29th Feb 2000 ==> 29th Feb 2000 (illegal) ==> 28th Feb 2001
  myDate := IncMonth(myDate, 12);      // Increment by 12 months
  ShowMessage('myDate + 12 months = '+DateToStr(myDate));
end;
Show full unit code
  myDate = 31/01/2000
  myDate + 1 month = 29/02/2000
  myDate + 12 months = 28/02/2001
 
Delphi Programming © Neil Moffatt 2002 - 2022. All rights reserved.  |  Home Page