DateTime.Subtract() Method in C# Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: csharp // 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); } } } Output: TimeSpan between date1 and date2 is 365.00:00:00 Example 2: csharp // 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); } } } 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: csharp // 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); } } } Output: DateTime between date and ts is 12/30/2010 15:44:59 Example 2: For ArgumentOutOfRangeException csharp // 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); } } } Output: Exception Thrown: System.ArgumentOutOfRangeException Reference: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.subtract?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article Decimal.Subtract() Method in C# R rohitprasad3 Follow Improve Article Tags : C# CSharp-method CSharp DateTime Struct Similar Reads Decimal.Subtract() Method in C# This method is used to subtract the one specified Decimal value from another. Syntax: public static decimal Subtract (decimal a1, decimal a2); Parameters: a1: This parameter specifies the minuend. a2: This parameter specifies the subtrahend. Return Value: Result of subtracting a2 from a1. Exceptions 2 min read DateTime.Add() Method in C# This method is used to return a new DateTime that adds the value of the specified TimeSpan to the value of this instance. Syntax: public DateTime Add (TimeSpan value); Here, value is a positive or negative time interval. Return Value: This method returns an object whose value is the sum of the date 2 min read DateTime.ToShortDateString() Method in C# This method is used to convert the value of the current DateTime object to its equivalent short date string representation. Syntax: public string ToShortDateString (); Return Value: This method returns a string that contains the short date string representation of the current DateTime object. Below 2 min read DateTime.ToString() Method in C# | Set â 1 This method is used to Converts the value of the current DateTime object to its equivalent string representation. There are total 4 methods in the overload list of this method: ToString(String, IFormatProvider)ToString(String)ToString(IFormatProvider)ToString() Here, we will discuss only first two m 8 min read DateTime.ToLongDateString() Method in C# This method is used to convert the value of the current DateTime object to its equivalent long date string representation. Syntax: public string ToLongDateString (); Return Value: This method returns a string that contains the long date string representation of the current DateTime object. Below pro 2 min read DateTimeOffset.ToOffset() Method in C# DateTimeOffset.ToOffset(TimeSpan) Method is used to convert the value of the current DateTimeOffset object to the date and time specified by an offset value. Syntax: public DateTimeOffset ToOffset (TimeSpan offset); Here, it takes the offset to convert the DateTimeOffset value to. Return Value: This 2 min read Like