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.