Sammenlikning av to datoer

C#:
Sammenlikning av to datoer

How to:

using System;

class DatesComparison {
    static void Main() {
        DateTime date1 = new DateTime(2023, 03, 15);
        DateTime date2 = new DateTime(2023, 04, 20);
        
        int comparison = DateTime.Compare(date1, date2);
        
        if (comparison < 0)
            Console.WriteLine("date1 er før date2.");
        else if (comparison == 0)
            Console.WriteLine("date1 er samme som date2.");
        else
            Console.WriteLine("date1 er etter date2.");
    }
}

Output:

date1 er før date2.

Deep Dive

Comparing dates in C# has been around since the .NET Framework’s inception, intrinsic to the DateTime structure. Alternatives include TimeSpan for duration calculations, and DateTimeOffset for timezone-aware comparisons. Using DateTime.Compare() is straightforward; it returns an integer signifying the relationship. Internally, dates are represented as ticks (100-nanosecond intervals since 12:00 midnight, January 1, 0001) enabling precise comparisons and calculations.

See Also