DateTime.Subtract() Method in C#

This method is used to subtract the specified time or duration from this instance. There are 2 methods in the overload list of this method as follows:

  • Subtract(DateTime)
  • Subtract(TimeSpan)

DateTime.Subtract(DateTime)

This method is used to subtract the specified date and time from this instance.

Syntax: public TimeSpan Subtract (DateTime value);

Return Value: This method returns a time interval that is equal to the date and time represented by this instance minus the date and time represented by value.

Exception: This method will give ArgumentOutOfRangeException if the result is less than MinValue or greater than MaxValue.



Below programs illustrate the use of DateTime.Subtract(DateTime) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.Subtract(DateTime)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // creating object of DateTime
            DateTime date1 = new DateTime(2011, 1,
                                     1, 4, 0, 15);
  
            // creating object of DateTime
            DateTime date2 = new DateTime(2010, 1,
                                     1, 4, 0, 15);
  
            // getting ShortTime from DateTime
            // using Subtract() method;
            TimeSpan value = date1.Subtract(date2);
  
            // Display the TimeSpan
            Console.WriteLine("TimeSpan between date1"+
                           " and date2 is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

TimeSpan between date1 and date2 is 365.00:00:00

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.Subtract(DateTime)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // creating object of DateTime
            DateTime date1 = DateTime.MinValue;
  
            // creating object of DateTime
            DateTime date2 = new DateTime(11119999, 1,
                                         1, 4, 0, 15);
  
            // getting ShortTime from DateTime
            // using Subtract() method;
            TimeSpan value = date1.Subtract(date2);
  
            // Display the TimeSpan
            Console.WriteLine("TimeSpan between date1 "+
                             "and date2 is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Exception Thrown: System.ArgumentOutOfRangeException

DateTime.Subtract(TimeSpan)

This method is used to subtract the specified duration from this instance.

Syntax: public DateTime Subtract (TimeSpan value);

Return Value: This method returns an object that is equal to the date and time represented by this instance minus the time interval represented by value.

Exception: This method will give ArgumentOutOfRangeException if the result is less than MinValue or greater than MaxValue.

Below programs illustrate the use of DateTime.Subtract(TimeSpan) Method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.Subtract(TimeSpan)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // creating object of DateTime
            DateTime date = new DateTime(2011, 1,
                                    1, 4, 0, 15);
  
            // creating object of TimeSpan
            TimeSpan ts = new TimeSpan(1, 12,
                                     15, 16);
  
            // getting ShortTime from 
            // subtracting DateTime and TimeSpan
            // using Subtract() method;
            DateTime value = date.Subtract(ts);
  
            // Display the TimeSpan
            Console.WriteLine("DateTime between date "+
                               "and ts is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

DateTime between date and ts is 12/30/2010 15:44:59

Example 2: For ArgumentOutOfRangeException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.Subtract(TimeSpan)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // creating object of DateTime
            DateTime date = DateTime.MinValue;
  
            // creating object of TimeSpan
            TimeSpan ts = new TimeSpan(1, 12, 15, 16);
  
            // getting ShortTime from subtracting 
            // DateTime and TimeSpan
            // using Subtract() method;
            DateTime value = date.Subtract(ts);
  
            // Display the TimeSpan
            Console.WriteLine("DateTime between date"+
                             " and ts is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e)
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Exception Thrown: System.ArgumentOutOfRangeException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.