Convert DateTimeOffset to the date and time specified by an offset value in CSharp

Description

The following code shows how to convert DateTimeOffset to the date and time specified by an offset value.

Example


//  w  w  w  .  j av a 2s.  c  om
using System;

public class DateTimeOffsetConversion
{
   public static void Main()
   {
      DateTimeOffset targetTime;
      DateTimeOffset sourceTime = new DateTimeOffset(2014, 9, 1, 9, 30, 0, 
                                      new TimeSpan(-5, 0, 0));

      targetTime = sourceTime.ToOffset(new TimeSpan(-5, 0, 0));
      
      Console.WriteLine("{0} converts to {1}", sourceTime, targetTime);
      Console.WriteLine("{0} and {1} are equal: {2}", 
                        sourceTime, targetTime, sourceTime.Equals(targetTime));
      Console.WriteLine("{0} and {1} are identical: {2}", 
                        sourceTime, targetTime, 
                        sourceTime.EqualsExact(targetTime)); 
   }
}

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