Manipulating DateTime values is one of the most common operations in a lot of applications. In this article, we are going to learn about the DateTime operators in C# that can come in handy when manipulating DateTime values in .NET (Core).

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

Addition

This operator returns a new DateTime value as a result of adding a time interval to a DateTime value.

To keep our examples simple, let’s define some variables that we’re going to use in all our examples:

var dt = new DateTime(2022, 1, 1);
var dt1 = new DateTime(2023, 1, 1);
var ts = new TimeSpan(20, 4, 2, 1);

The dt object holds a value of 01/01/2022 12:00:00 AM while the ts object holds a value of 20 days, 4 hours, 2 minutes, and 1 second.

There are different ways that can come in handy when adding two DateTime values.

We can implement it by adding two DateTime values using the + sign or we can make use of the inbuilt Add function. We can use the inbuilt methods AddDays, AddYears, AddMinutes, and AddSeconds functions to achieve the same result:

var expected = new DateTime(2022, 1, 21, 4, 2, 1);
Assert.AreEqual(expected, dt + ts);
Assert.AreEqual(expected, dt.Add(ts));
Assert.AreEqual(expected, dt.AddDays(20).AddHours(4).AddMinutes(2).AddSeconds(1));

Equality

As the name suggests, this operator assesses whether two DateTime values are equal and returns  true if they are.

We can implement this operator by using the == sign, or we can compare two DateTime values using an inbuilt Equals function:

var dt = new DateTime(2022, 1, 1);

Assert.IsTrue(dt == dt);
Assert.IsTrue(dt.Equals(dt));

GreaterThan

This operator assesses whether a given DateTime value is later than another given DateTime value. The operator returns true if it is. 

We can implement this operator by using the > sign or we can utilize the inbuilt Compare and CompareTo functions:

Assert.IsTrue(dt1 > dt);
Assert.AreEqual(1, DateTime.Compare(dt1, dt));
Assert.AreEqual(1, dt1.CompareTo(dt));

These functions compare two DateTime values and return int values. A value of 1 means that a date value 01/01/2023 12:00:00 AM is later than a date 01/01/2022 12:00:00 AM.

GreaterThanOrEqual

The goal of this operator is to assess whether a given DateTime value is later than or equal to another given DateTime value, and if it is, the result is true

We can implement this operator either by using the >= sign or by using the inbuilt Compare and CompareTo functions:

Assert.IsTrue(dt1 >= dt);
Assert.AreEqual(0, DateTime.Compare(dt, dt));
Assert.AreEqual(0, dt.CompareTo(dt));

A value of 0 means two dates 01/01/2022 12:00:00 AM and 01/01/2022 12:00:00 AM are equal. On the other hand, a value of 1 means that a date value 01/01/2023 12:00:00 AM is later than a date 01/01/2022 12:00:00 AM.

Inequality

As the name of the operator suggests, this operator checks whether two DateTime values are not equal. If the two DateTime values are not equal, this operator returns true.

We can implement this operator either by using the != sign or by negating the value of the Equals inbuilt function:

Assert.IsTrue(dt != dt1);
Assert.IsTrue(!dt.Equals(dt1));

LessThan

This operator assesses whether a given DateTime value is earlier than another DateTime value, and if it is, the operator returns true

We can implement this operator in two ways, the first being by using the < operator or we can use the inbuilt Compare and CompareTo functions that we’ve used for GreaterThan and GreaterThanOrEqual:

Assert.IsTrue(dt < dt1);
Assert.AreEqual(-1, DateTime.Compare(dt, dt1));
Assert.AreEqual(-1, dt.CompareTo(dt1));

A value of -1 means, a date 01/01/2022 12:00:00 AM  is less than a later date 01/01/2023 12:00:00 AM.

LessThanOrEqual

This operator assesses whether a given DateTime value is earlier than or equal to another DateTime value, and if it is, the operator returns true

This operator can be implemented using the <= sign, or we can use the inbuilt Compare and CompareTo here as well:

Assert.IsTrue(dt <= dt1);
Assert.AreEqual(0, DateTime.Compare(dt, dt));
Assert.AreEqual(0, dt.CompareTo(dt));

Subtraction

This operator subtracts a DateTime value or a TimeSpan value from another DateTime value and it has two overloads.

APIDescriptionRequest bodyRequest response
POST api/resourcesCreate a new resourceResourceCreated resource

Just as the addition operator, we can implement the subtraction operator by using the - sign or the Subtract inbuilt function as. On top of that, we can also pass the negative TimeSpan value while using the inbuilt Add function:

var expected = new DateTime(2021, 12, 11, 19, 57, 59);

Assert.AreEqual(expected, dt - ts);
Assert.AreEqual(expected, dt.Subtract(ts));
Assert.AreEqual(expected, dt.Add(-ts));

The result of subtracting a TimeSpan value from a DateTime value is a DateTime object whose value is 12/11/2021 7:57:59 PM in this case. 

Conclusion

We’ve learned how to manipulate DateTime values with DateTime operators in C#. The article discusses how to manipulate those values through operators + and - as well as through the use of inbuilt functions Add,Subtract,Compare, etc.