C# TimeSpan Compare

Description

TimeSpan Compare compares two TimeSpan values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value.

Syntax

TimeSpan.Compare has the following syntax.


public static int Compare(
  TimeSpan t1,
  TimeSpan t2
)

Parameters

TimeSpan.Compare has the following parameters.

  • t1 - The first time interval to compare.
  • t2 - The second time interval to compare.

Returns

TimeSpan.Compare method returns One of the following values. Value Description -1 t1 is shorter than t2. 0 t1 is equal to t2. 1 t1 is longer than t2.

Example

The following example uses the Compare method to compare TimeSpan objects with a TimeSpan object.


/*  w  w  w.  j  av a 2  s. c  o m*/

using System;

class Example
{
    static void Main()
    {
        // Define a time interval equal to two hours.
        TimeSpan baseInterval = new TimeSpan( 2, 0, 0);

        TimeSpan span = TimeSpan.FromSeconds(-2.5);

        int result = TimeSpan.Compare(baseInterval, span);
        Console.WriteLine(result);     
    } 
} 

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version