TimeSpan.Compare() Method in C#

The TimeSpan.Compare() method in C# is a static method used to compare two TimeSpan values and returns an integer that indicates their relative relationship. This method is useful for sorting operations and determining the order of time intervals.

Syntax

Following is the syntax for the TimeSpan.Compare() method −

public static int Compare(TimeSpan span1, TimeSpan span2);

Parameters

  • span1 − The first TimeSpan value to compare.

  • span2 − The second TimeSpan value to compare.

Return Value

The method returns an integer with the following meaning −

Return Value Condition Meaning
-1 span1 < span2 First TimeSpan is shorter than the second
0 span1 = span2 Both TimeSpan values are equal
1 span1 > span2 First TimeSpan is longer than the second

TimeSpan.Compare() Return Values Returns -1 span1 < span2 First is shorter Returns 0 span1 = span2 Values are equal Returns 1 span1 > span2 First is longer

Example - Comparing Different TimeSpan Values

This example demonstrates comparing various TimeSpan values −

using System;

public class Demo {
    public static void Main() {
        TimeSpan span1 = new TimeSpan(2, 30, 0);  // 2 hours, 30 minutes
        TimeSpan span2 = new TimeSpan(1, 45, 0);  // 1 hour, 45 minutes
        TimeSpan span3 = new TimeSpan(2, 30, 0);  // Same as span1
        
        int result1 = TimeSpan.Compare(span1, span2);
        int result2 = TimeSpan.Compare(span2, span1);
        int result3 = TimeSpan.Compare(span1, span3);
        
        Console.WriteLine("span1: " + span1);
        Console.WriteLine("span2: " + span2);
        Console.WriteLine("span3: " + span3);
        Console.WriteLine();
        Console.WriteLine("Compare(span1, span2): " + result1);
        Console.WriteLine("Compare(span2, span1): " + result2);
        Console.WriteLine("Compare(span1, span3): " + result3);
    }
}

The output of the above code is −

span1: 02:30:00
span2: 01:45:00
span3: 02:30:00

Compare(span1, span2): 1
Compare(span2, span1): -1
Compare(span1, span3): 0

Example - Using Compare for Sorting

This example shows how to use TimeSpan.Compare() for sorting time intervals −

using System;

public class Demo {
    public static void Main() {
        TimeSpan[] intervals = {
            new TimeSpan(0, 45, 0),    // 45 minutes
            new TimeSpan(2, 15, 0),    // 2 hours 15 minutes
            new TimeSpan(1, 0, 0),     // 1 hour
            new TimeSpan(0, 30, 0)     // 30 minutes
        };
        
        Console.WriteLine("Original order:");
        foreach (TimeSpan ts in intervals) {
            Console.WriteLine(ts);
        }
        
        Array.Sort(intervals, TimeSpan.Compare);
        
        Console.WriteLine("\nSorted order:");
        foreach (TimeSpan ts in intervals) {
            Console.WriteLine(ts);
        }
    }
}

The output of the above code is −

Original order:
00:45:00
02:15:00
01:00:00
00:30:00

Sorted order:
00:30:00
00:45:00
01:00:00
02:15:00

Example - Handling Negative TimeSpan Values

This example demonstrates comparison with negative TimeSpan values −

using System;

public class Demo {
    public static void Main() {
        TimeSpan positiveSpan = new TimeSpan(1, 30, 0);    // 1.5 hours
        TimeSpan negativeSpan = new TimeSpan(-1, -30, 0);  // -1.5 hours
        TimeSpan zeroSpan = TimeSpan.Zero;
        
        Console.WriteLine("Positive span: " + positiveSpan);
        Console.WriteLine("Negative span: " + negativeSpan);
        Console.WriteLine("Zero span: " + zeroSpan);
        Console.WriteLine();
        
        Console.WriteLine("Compare(positive, negative): " + TimeSpan.Compare(positiveSpan, negativeSpan));
        Console.WriteLine("Compare(negative, zero): " + TimeSpan.Compare(negativeSpan, zeroSpan));
        Console.WriteLine("Compare(zero, positive): " + TimeSpan.Compare(zeroSpan, positiveSpan));
    }
}

The output of the above code is −

Positive span: 01:30:00
Negative span: -01:30:00
Zero span: 00:00:00

Compare(positive, negative): 1
Compare(negative, zero): -1
Compare(zero, positive): -1

Conclusion

The TimeSpan.Compare() method provides a reliable way to compare two time intervals, returning -1, 0, or 1 based on their relative values. This method is particularly useful for sorting operations and conditional logic involving time durations.

Updated on: 2026-03-17T07:04:36+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements