Add, Subtract, and Compare Dates and Times - CSharp Language Basics

CSharp examples for Language Basics:Date Time

Introduction

Operators Supported by DateTime and TimeSpan

Operator
TimeSpan
DateTime
Assignment ( =)


Because TimeSpan is a structure,
assignment returns a copy, not a
reference
Because DateTime is a structure,
assignment returns a copy, not a
reference
Addition (+)

Adds two TimeSpan instances

Adds a TimeSpan instance to a DateTime
instance
Subtraction (-)


Subtracts one TimeSpan instance from
another TimeSpan instance

Subtracts a TimeSpan instance or a
DateTime instance from a DateTime
instance
Equality (==)

Compares two TimeSpan instances and
returns true if they are equal
Compares two DateTime instances and
returns true if they are equal
Inequality ( !=)

Compares two TimeSpan instances and
returns true if they are not equal
Compares two DateTime instances and
returns true if they are not equal
Greater than (>)

Determines if one TimeSpan instance is
greater than another TimeSpan instance
Determines if one DateTime instance is
greater than another DateTime instance
Greater than or
equal to (>=)

Determines if one TimeSpan instance is
greater than or equal to another
TimeSpan instance
Determines if one DateTime instance is
greater than or equal to another
DateTime instance
Less than (<)

Determines if one TimeSpan instance is
less than another TimeSpan instance
Determines if one DateTime instance is
less than another DateTime instance
Less than or
equal to (<=)

Determines if one TimeSpan instance is
less than or equal to another TimeSpan

Determines if one DateTime instance is
less than or equal to another DateTime
instance
Unary negation
(-)

Returns a TimeSpan instance with a
negated value of the specified TimeSpan
instance
Not supported


Unary plus (+)
Returns the TimeSpan instance specified
Not supported

Demo Code

using System;//from ww w . ja va2s  . c  om
class MainClass
{
   public static void Main()
   {
      // Create a TimeSpan representing 2.5 days.
      TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0);
      // Create a TimeSpan representing 4.5 days.
      TimeSpan timespan2 = new TimeSpan(4, 12, 0, 0);
      // create a TimeSpan representing 3.2 days
      // using the static convenience method
      TimeSpan timespan3 = TimeSpan.FromDays(3.2);
      // Create a TimeSpan representing 1 week.
      TimeSpan oneWeek = timespan1 + timespan2;
      // Create a DateTime with the current date and time.
      DateTime now = DateTime.Now;
      // Create a DateTime representing 1 week ago.
      DateTime past = now - oneWeek;
      // Create a DateTime representing 1 week in the future.
      DateTime future = now + oneWeek;
      // Display the DateTime instances.
      Console.WriteLine("Now   : {0}", now);
      Console.WriteLine("Past  : {0}", past);
      Console.WriteLine("Future: {0}", future);
      // use the comparison operators
      Console.WriteLine("Now is greater than past: {0}", now > past);
      Console.WriteLine("Now is equal to future: {0}", now == future);
   }
}

Result


Related Tutorials