Home  |  Delphi .net Home  |  System.Globalization.Calendar  |  AddYears Method
AddYears  
Method  
Add a number of Years to a DateTime value
Calendar Class
System.Globalization NameSpace
CF1.  Function AddYears ( TheDateTime:DateTimeTheDateTime : DateTime; Years : Integer; ) : DateTime;
CF : Methods with this mark are Compact Framework Compatible
Description
Adds Years to TheDateTime, and returns a new DateTime with this value.
 
If the day of the month is no longer valid (see the example) it is reduced accordingly.
References
DateTime
CultureInfo
Microsoft MSDN Links
System.Globalization
System.Globalization.Calendar
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Globalization;

var
  myDate    : DateTime;

  gbCulture : System.Globalization.CultureInfo;
  gbCal     : System.Globalization.Calendar;

begin
  // Set up a Great Britain English culture & calendar
  gbCulture := System.Globalization.CultureInfo.Create('en-GB');
  gbCal     := gbCulture.Calendar;

  // Create a DateTime object : use the gbCalendar to control the
  // conversion of year, month and day parameter values into the
  // number of ticks since 1st January 0001 AD (the internal value)

  // 29th February 2004
  myDate := DateTime.Create(2004, 2, 29, gbCal);

  // Display the date
  Console.WriteLine(myDate.ToLongDateString);

  Console.WriteLine;
  Console.WriteLine('Adding 1 year : ');
  Console.WriteLine;

  // Add 1 year to the date
  myDate := gbCal.AddYears(myDate, 1);

  // Display the date
  Console.WriteLine(myDate.ToLongDateString);

  Console.ReadLine;
end.
Show full unit code
  29 February 2004
  
  Adding 1 year :
  
  28 February 2005
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author