C# DateTime Subtraction(DateTime, DateTime)
Description
DateTime Subtraction(DateTime, DateTime)
subtracts
a specified date and time from another specified date and time and returns
a time interval.
Syntax
DateTime.Subtraction(DateTime, DateTime)
has the following syntax.
public static TimeSpan operator -(
DateTime d1,
DateTime d2
)
Parameters
DateTime.Subtraction(DateTime, DateTime)
has the following parameters.
d1
- The date and time value to subtract from (the minuend).d2
- The date and time value to subtract (the subtrahend).
Returns
DateTime.Subtraction(DateTime, DateTime)
method returns The time interval between d1 and d2; that is, d1 minus d2.
Example
The following example demonstrates the Subtract method and the subtraction operator.
using System;// www.java 2 s. c om
public class MainClass{
public static void Main(String[] argv){
System.DateTime date1 = new System.DateTime(2014, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(2014, 12, 6, 13, 2, 0);
System.DateTime date3 = new System.DateTime(2014, 10, 12, 8, 42, 0);
// diff1 gets 185 days, 14 hours, and 47 minutes.
System.TimeSpan diff1 = date2.Subtract(date1);
System.Console.WriteLine(diff1);
// date4 gets 4/9/2014 5:55:00 PM.
System.DateTime date4 = date3.Subtract(diff1);
System.Console.WriteLine(date4);
// diff2 gets 55 days 4 hours and 20 minutes.
System.TimeSpan diff2 = date2 - date3;
System.Console.WriteLine(diff2);
// date5 gets 4/9/2014 5:55:00 PM.
System.DateTime date5 = date1 - diff2;
System.Console.WriteLine(date5);
}
}
The code above generates the following result.