TimeSpan.Compare Method

May 11, 2014

Compares two TimeSpan values and returns an integer that indicates whether the first time interval is longer than, equal to, or shorter than the second time interval.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static int Compare(
	TimeSpan t1,
	TimeSpan t2
)

Parameters

t1
Type: System.TimeSpan
The first time interval to compare.
t2
Type: System.TimeSpan
The second time interval to compare.

Return Value

Type: System.Int32
One of the values in the following table.

Value

Description

-1

t1 is shorter than t2

0

t1 is equal to t2

1

t1 is longer than t2

The following code example compares several TimeSpan objects to a reference TimeSpan using the Compare method.


// Example of the TimeSpan.Compare( TimeSpan, TimeSpan ) and 
// TimeSpan.Equals( TimeSpan, TimeSpan ) methods.
using System;

class Example
{
   const string dataFmt = "{0,-38}{1}";

   // Compare TimeSpan parameters, and display them with the results.
   static void CompareTimeSpans(System.Windows.Controls.TextBlock outputBlock, TimeSpan Left, TimeSpan Right,
       string RightText)
   {
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format(dataFmt, "Right: " + RightText, Right) + "\n";
      outputBlock.Text += String.Format(dataFmt, "TimeSpan.Equals( Left, Right )",
          TimeSpan.Equals(Left, Right)) + "\n";
      outputBlock.Text += String.Format(dataFmt,
          "TimeSpan.Compare( Left, Right )",
          TimeSpan.Compare(Left, Right)) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      TimeSpan Left = new TimeSpan(2, 0, 0);

      outputBlock.Text += 
          "This example of the TimeSpan.Equals( TimeSpan, Time" +
          "Span ) and \nTimeSpan.Compare( TimeSpan, TimeSpan ) " +
          "methods generates the \nfollowing output by creating " +
          "several different TimeSpan \nobjects and comparing " +
          "them with a 2-hour TimeSpan.\n" + "\n";
      outputBlock.Text += String.Format(dataFmt, "Left: TimeSpan( 2, 0, 0 )",
          Left) + "\n";

      // Create objects to compare with a 2-hour TimeSpan.
      CompareTimeSpans(outputBlock, Left, new TimeSpan(0, 120, 0),
          "TimeSpan( 0, 120, 0 )");
      CompareTimeSpans(outputBlock, Left, new TimeSpan(2, 0, 1),
          "TimeSpan( 2, 0, 1 )");
      CompareTimeSpans(outputBlock, Left, new TimeSpan(2, 0, -1),
          "TimeSpan( 2, 0, -1 )");
      CompareTimeSpans(outputBlock, Left, new TimeSpan(72000000000),
          "TimeSpan( 72000000000 )");
      CompareTimeSpans(outputBlock, Left, TimeSpan.FromDays(1.0 / 12D),
          "TimeSpan.FromDays( 1 / 12 )");
   }
}

/*
This example of the TimeSpan.Equals( TimeSpan, TimeSpan ) and
TimeSpan.Compare( TimeSpan, TimeSpan ) methods generates the
following output by creating several different TimeSpan
objects and comparing them with a 2-hour TimeSpan.

Left: TimeSpan( 2, 0, 0 )             02:00:00

Right: TimeSpan( 0, 120, 0 )          02:00:00
TimeSpan.Equals( Left, Right )        True
TimeSpan.Compare( Left, Right )       0

Right: TimeSpan( 2, 0, 1 )            02:00:01
TimeSpan.Equals( Left, Right )        False
TimeSpan.Compare( Left, Right )       -1

Right: TimeSpan( 2, 0, -1 )           01:59:59
TimeSpan.Equals( Left, Right )        False
TimeSpan.Compare( Left, Right )       1

Right: TimeSpan( 72000000000 )        02:00:00
TimeSpan.Equals( Left, Right )        True
TimeSpan.Compare( Left, Right )       0

Right: TimeSpan.FromDays( 1 / 12 )    02:00:00
TimeSpan.Equals( Left, Right )        True
TimeSpan.Compare( Left, Right )       0
*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft