Add a time interval to a DateTimeOffset in CSharp

Description

The following code shows how to add a time interval to a DateTimeOffset.

Example


//  ww  w. j  av  a 2 s  .com
using System;
public class MainClass{
  public static void Main(String[] argv){  
    DateTimeOffset date1 = new DateTimeOffset(2014, 1, 1, 13, 32, 45, 
                           new TimeSpan(-5, 0, 0)); 
    TimeSpan interval1 = new TimeSpan(2, 3, 30, 0);
    TimeSpan interval2 = new TimeSpan(5, 0, 0, 0);      
    DateTimeOffset date2; 
    
    Console.WriteLine(date1);        
    date2 = date1 + interval1;
    Console.WriteLine(date2);      
    date2 += interval2;
    Console.WriteLine(date2);      
  }
}

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