Subtract DateTimeOffset from another DateTimeOffset and yields a time interval in CSharp

Description

The following code shows how to subtract DateTimeOffset from another DateTimeOffset and yields a time interval.

Example


/*ww  w .  j a  v a  2  s  .  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 »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var