Assert DateTime the Right Way MSTest NUnit C# Code

Assert DateTime the Right Way MSTest NUnit C# Code

How to Assert DateTime in NUnit

NUnit has added built-in support for this using the keyword Within.

DateTime now = DateTime.Now;
DateTime later = now + TimeSpan.FromHours(1.0);

Assert.That(later.Is.EqualTo(now).Within(TimeSpan.FromHours(3.0));
Assert.That(later, Is.EqualTo(now).Within(3).Hours;

So you don’t need to write a logic to test DateTimes.

How to Assert DateTime in MSTest

You will need an enum and a static class. The enum will hold the delta DateTime types- Seconds, Minutes, etc.

public enum DateTimeDeltaType
{
    Days,
    Minutes
}

Next use the below class to assert DateTimes with particular delta count.

public static class DateTimeAssert
{
    public static void AreEqual(DateTime? expectedDate, DateTime? actualDate, DateTimeDeltaType deltaType, int count)
    {
        if (expectedDate == null && actualDate == null)
        {
            return;
        }
        else if (expectedDate == null)
        {
            throw new NullReferenceException("The expected date was null");
        }
        else if (actualDate == null)
        {
            throw new NullReferenceException("The actual date was null");
        }
        TimeSpan expectedDelta = GetTimeSpanDeltaByType(deltaType, count);
        double totalSecondsDifference = Math.Abs(((DateTime)actualDate – (DateTime)expectedDate).TotalSeconds);

        if (totalSecondsDifference > expectedDelta.TotalSeconds)
        {
            throw new Exception(string.Format("Expected Date: {0}, Actual Date: {1} \nExpected Delta: {2}, Actual Delta in seconds- {3} (Delta Type: {4})",
                                            expectedDate,
                                            actualDate,
                                            expectedDelta,
                                            totalSecondsDifference,
                                            deltaType));
        }
    }

    private static TimeSpan GetTimeSpanDeltaByType(DateTimeDeltaType type, int count)
    {
        TimeSpan result = default(TimeSpan);

        switch (type)
        {
            case DateTimeDeltaType.Days:
                result = new TimeSpan(count, 0, 0, 0);
                break;
            case DateTimeDeltaType.Minutes:
                result = new TimeSpan(0, count, 0);
                break;
            default: throw new NotImplementedException("The delta type is not implemented.");
        }

        return result;
    }
}
DateTimeAssert.AreEqual(
    new DateTime(2014, 10, 10, 20, 22, 16),
    new DateTime(2014, 10, 11, 20, 22, 16),
    DateTimeDeltaType.Days,
    1);

Improved Version

Almost half year after initial publication, I created an even better implementation of the class. Credits go to my great readers in CodeProject. Thank you!

public static class DateTimeAssert
{
    public static void AreEqual(DateTime? expectedDate, DateTime? actualDate, TimeSpan maximumDelta)
    {
        if (expectedDate == null && actualDate == null)
        {
            return;
        }
        else if (expectedDate == null)
        {
            throw new NullReferenceException("The expected date was null");
        }
        else if (actualDate == null)
        {
            throw new NullReferenceException("The actual date was null");
        }
        double totalSecondsDifference = Math.Abs(((DateTime)actualDate – (DateTime)expectedDate).TotalSeconds);

        if (totalSecondsDifference > maximumDelta.TotalSeconds)
        {
            throw new Exception(string.Format("Expected Date: {0}, Actual Date: {1} \nExpected Delta: {2}, Actual Delta in seconds- {3}",
                                            expectedDate,
                                            actualDate,
                                            maximumDelta,
                                            totalSecondsDifference));
        }
    }
}

This version usage is more expressive and gives the freedom to use any Delta:

DateTimeAssert.AreEqual(
    new DateTime(2014, 10, 10, 20, 22, 16),
    new DateTime(2014, 10, 11, 20, 22, 16),
    TimeSpan.FromMilliSeconds(500);
DateTimeAssert.AreEqual(
    new DateTime(2014, 10, 10, 20, 22, 16),
    new DateTime(2014, 10, 11, 20, 22, 16),
    TimeSpan.FromMinutes(0.5); // half a minute = 30s

Related Articles

Development

Top 15 Underutilized Features of .NET Part 2

In this article, I'm going to share with you even more underutilized features of C# language (underutilized). If you missed the first publication you should che

Top 15 Underutilized Features of .NET Part 2

Development

Neat Tricks for Effortlessly Formatting Currency in C#

In the article, I am going to present to you a few ways how to create a handy class that effortlessly formatting currency in C#. Initially, I came up with the i

Neat Tricks for Effortlessly Formatting Currency in C#

Development

Embracing Non-Nullable Reference Types in C# 8

Microsoft launched its latest version of the C# language – C# 8.0 – in September 2019. It came with many new features or improvements like readonly struct membe

Embracing Non-Nullable Reference Types in C# 8

Development

Types Of Code Coverage- Examples In C#

Code coverage analysis is used to measure the quality of software testing, usually using dynamic execution flow analysis. There are many different types of code

Types Of Code Coverage- Examples In C#

Development

Specification-based Test Design Techniques for Enhancing Unit Tests

The primary goal of most developers is usually achieving 100% code coverage if they write any unit tests at all. In this test design how-to article, I am going

Specification-based Test Design Techniques for Enhancing Unit Tests

Development

Get Property Names Using Lambda Expressions in C#

In this article, I am going to present to you how to get property and method names from lambda expressions. You can pass properties and methods as methods' para

Get Property Names Using Lambda Expressions in C#
Anton Angelov

About the author

Anton Angelov is Managing Director, Co-Founder, and Chief Test Automation Architect at Automate The Planet — a boutique consulting firm specializing in AI-augmented test automation strategy, implementation, and enablement. He is the creator of BELLATRIX, a cross-platform framework for web, mobile, desktop, and API testing, and the author of 8 bestselling books on test automation. A speaker at 60+ international conferences and researcher in AI-driven testing and LLM-based automation, he has been recognized as QA of the Decade and Webit Changemaker 2025.