Home  |  Delphi .net Home  |  System.DateTime  |  Subtract Method
Subtract  
Method  
Subtract a DateTime or TimeSpan from the current DateTime value
DateTime Class
System NameSpace
CF1.  Function Subtract ( Value : DateTime; ) : TimeSpan ;
CF2.  Function Subtract ( Value : TimeSpan; ) : DateTime;
CF : Methods with this mark are Compact Framework Compatible
Description
The Subtract method does two very different things :
 
1. Returns a TimeSpan value as the difference between the current DateTime value and the Value DateTime value. The TimeSpan value contains a day and time value - the value is a total elapsed time, not a point in time.
 
2. Returns a DateTime value. You provide a day/time span, which gets subtracted from the current DateTime value to give the return value.
Microsoft MSDN Links
System
System.DateTime
 
 
A simple example illustrating both methods
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
program Project1;
{$APPTYPE CONSOLE}

var
  dateTime1, dateTime2 : DateTime;
  timeSpan1            : TimeSpan;

begin
  dateTime1 := DateTime.Create(2004, 6, 20);  // 00:00:00 on 20 June 2004
  dateTime2 := DateTime.Create(2004, 7, 11);  // 00:00:00 on 11 July 2004

  Console.WriteLine('dateTime1       = {0:F}', dateTime1);
  Console.WriteLine('dateTime2       = {0:F}', dateTime2);
  Console.WriteLine;
  Console.WriteLine('Subtracting dateTime2 from dateTime1');
  Console.WriteLine;

  timeSpan1 := dateTime2.Subtract(dateTime1);

  Console.WriteLine('Gives time span = {0:F}', timeSpan1);
  Console.WriteLine;

  Console.WriteLine('Subtracting this time span from dateTime1');
  Console.WriteLine;

  dateTime1 := dateTime1.Subtract(timeSpan1);

  Console.WriteLine('dateTime1 now   = {0:F}', dateTime1);

  Console.ReadLine;
end.
 
end.
Hide full unit code
  dateTime1       = 20 June 2004 00:00:00
  dateTime2       = 11 July 2004 00:00:00
  
  Subtracting dateTime2 from dateTime1
  
  Gives time span = 21.00:00:00
  
  Subtracting this time span from dateTime1
  
  dateTime1 now   = 30 May 2004 00:00:00
  
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author