Home  |  Delphi .net Home  |  System.DateTime  |  Subtract Method
  
Subtract  
Method  
Subtract a DateTime or TimeSpan from the current DateTime value
DateTime Structure
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
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example illustrating both methods
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.
   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 2002 - 2017. All rights reserved.  |  Contact the author