C# - TimeSpan.Subtract() Method with Example

In this tutorial, we will learn about the C# TimeSpan.Subtract() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 30, 2023

TimeSpan.Subtract() Method

The TimeSpan.Subtract() method is used to return an object that contains subtracted value, here the specified object is subtracted from the current object.

Syntax

TimeSpan TimeSpan.Subtract(TimeSpan obj);

Parameter(s)

  • obj : An object whose value to be subtracted with a value of the current object.

Return Value

This method returns subtracted value, here the specified object is subtracted from the current object.

Exception(s)

  • System.OverflowException

C# Example of TimeSpan.Subtract() Method

The source code to demonstrate the use of Subtract() method of TimeSpan structure is given below. The given program is compiled and executed successfully.

using System;

class TimeSpanDemo {
  //Entry point of Program
  static public void Main() {
    //Here we create timespan with day,hour,minutes and seconds
    TimeSpan timespan1 = new TimeSpan(4, 2, 20, 0);
    TimeSpan timespan2 = new TimeSpan(2, 2, 10, 0);
    TimeSpan timespan3;

    timespan3 = timespan1.Subtract(timespan2);

    //Subtraction of timespan1 and timespan2 
    Console.WriteLine("Subtraction of timespan1 from timespan2 : " + timespan3);
  }
}

Output

Subtraction of timespan1 from timespan2 : 2.00:10:00
Press any key to continue . . .

C# TimeSpan Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.