Javatpoint Logo
Javatpoint Logo

TimeSpan.Subtract() Method in C#

In C#, the TimeSpan.Subtract method is a member of the TimeSpan struct, and it is used to subtract one TimeSpan from another. The method returns a new TimeSpan representing the result of the subtraction.

Purpose:

The primary purpose of TimeSpan.Subtract() is to find the difference between two-time durations. It enables developers to work with time intervals arithmetically and produce a new TimeSpan instance representing the result of the subtraction.

Syntax:

It has the following syntax:

Parameters

  • ts: It is the TimeSpan to subtract from the current instance. It represents the duration that will be deducted from the current TimeSpan instance.

Return Value

The method returns a new TimeSpan instance representing the result of subtracting the specified TimeSpan (ts) from the current instance.

Behaviour

The original instances involved in the operation are not modified by the Subtract() method; instead, a new TimeSpan instance is returned.

A negative TimeSpan will occur if the duration indicated by 'ts' exceeds the duration of the current instance.

The resultant TimeSpan can represent both positive and negative durations depending on which order the subtraction is done.

Use Cases

  • Calculating the duration between two points in time.
  • Handling scenarios requires finding the time difference between two DateTime instances.
  • Time-based arithmetic, such as determining intervals for scheduling or tracking elapsed time.

Considerations

  • The result may be negative if the TimeSpan being subtracted is greater than the original TimeSpan.
  • The Subtract() method is useful in various scenarios involving time calculations, making it a fundamental part of time manipulation in C#.

Example:

Let us take an example to illustrate the Timespan.Subtract() method in C#.

Output:

 
Original TimeSpan 1: 06:35:12
TimeSpan 2 to subtract: 08:40:18
Result after subtraction: -02:05:06

Explanation:

1. Creation of TimeSpan Instances

The TimeSpan constructor produces two TimeSpan instances, time_Span1 and time_Span2.

Time_Span1 represents 6 hours, 35 minutes, and 12 seconds.

The duration of 8 hours, 40 minutes, and 18 seconds is represented by time_Span2.

2. Subtract Method

The difference between time_Span1 and time_Span2 can be found using the Subtract() function.

The result is stored in the 'result' variable.

The Subtract() function creates and returns a new TimeSpan that represents the result of the subtraction rather than modifying the initial TimeSpan instances.

3. Displaying Results

The Console shows the original TimeSpan values (time_Span1 and time_Span2) and the subtracted value (result).WriteLine().

4. Console Output

The program prints the original durations and the result of the subtraction to the Console.

Exception

This method will give OverflowException when the resulting TimeSpan is smaller than the smallest possible value or greater than the largest possible value.

Example:

Let us take an example to illustrate the Timespan.Subtract() method using exception in C#.

Output:

 
Exception Thrown: System.OverflowException

Explanation:

1. Using Directives

The using System; directive includes the System namespace, which contains fundamental classes and base types.

2. Class Declaration

The Demo class has been declared. This class contains the Main method, which serves as the entry point for the program.

3. Try Block

The try block contains code attempts to execute. The catch block will handle any exceptions that may arise.

4. TimeSpan Objects

T1 and T2 are the names of the two TimeSpan objects created.

With TimeSpan, t1 is initialized. MinValue represents TimeSpan's minimum possible value.

4 days, 23 hours, 46 minutes, and 28 seconds are the specific duration initialized for t2.

5. Subtract Method

With t2 as the parameter, the Subtract method is called on the 't1' TimeSpan object. The result is stored in the 'vari_able' variable.

6. Console Output

Using the Console, the program prints the subtraction result to the Console.WriteLine.

7. Catch Block (OverflowException)

The catch block will catch an OverflowException if it happens during execution (for example, if the subtraction result exceeds the allowed range for TimeSpan).

After that, the program prints information about the exception, including its type and message.

8. Console Output (Example)

The program prints the resulting TimeSpan to the Console if there is no exception. The value of vari_able is displayed using the format string {0}.

Conclusion:

In conclusion, this program demonstrates the use of the TimeSpan class in C# to perform subtraction between two TimeSpan instances and handles potential overflow exceptions that might occur during the operation. The provided example uses TimeSpan.MinValue will showcase a scenario where an overflow might occur due to the subtraction.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA