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
IncDay Increments a TDateTime variable by + or - number of days
IncMinute Increments a TDateTime variable by + or - number of minutes
IncYear Increments a TDateTime variable by a number of years
IncSecond Increments a TDateTime variable by + or - number of seconds
IncMillisecond Increments a TDateTime variable by + or - number of milliseconds
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

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




 
Example code : Add values to an example 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
  SysUtils,   // Unit containing the IncMonth command
  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
  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;
 
end.
Hide full unit code
   myDate = 31/01/2000
   myDate + 1 month = 29/02/2000
   myDate + 12 months = 28/02/2001
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page