C# DateTimeOffset Subtraction(DateTimeOffset, DateTimeOffset)

Description

DateTimeOffset Subtraction(DateTimeOffset, DateTimeOffset) subtracts one DateTimeOffset object from another and yields a time interval.

Syntax

DateTimeOffset.Subtraction(DateTimeOffset, DateTimeOffset) has the following syntax.


public static TimeSpan operator -(
  DateTimeOffset left,
  DateTimeOffset right
)

Parameters

DateTimeOffset.Subtraction(DateTimeOffset, DateTimeOffset) has the following parameters.

  • left - The minuend.
  • right - The subtrahend.

Returns

DateTimeOffset.Subtraction(DateTimeOffset, DateTimeOffset) method returns An object that represents the difference between left and right.

Example

The Subtraction method defines the subtraction operation for DateTimeOffset objects. It enables code such as the following:


//  ww  w .j  a  v  a  2s  .  co  m
using System;
public class MainClass{
  public static void Main(String[] argv){  
    DateTimeOffset firstDate = new DateTimeOffset(2014, 3, 25, 18, 0, 0, 
                                                  new TimeSpan(-7, 0, 0));
    DateTimeOffset secondDate = new DateTimeOffset(2014, 3, 25, 18, 0, 0, 
                                                   new TimeSpan(-5, 0, 0));
    TimeSpan difference;
    
    difference = firstDate - secondDate;
    Console.WriteLine("({0}) - ({1}): {2} days, {3}:{4:d2}", 
                      firstDate.ToString(), 
                      secondDate.ToString(), 
                      difference.Days, 
                      difference.Hours, 
                      difference.Minutes);      

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version