C# DateTimeOffset ToOffset

Description

DateTimeOffset ToOffset converts the value of the current DateTimeOffset object to the date and time specified by an offset value.

Syntax

DateTimeOffset.ToOffset has the following syntax.


public DateTimeOffset ToOffset(
  TimeSpan offset
)

Parameters

DateTimeOffset.ToOffset has the following parameters.

  • offset - The offset to convert the DateTimeOffset value to.

Returns

DateTimeOffset.ToOffset method returns An object that is equal to the original DateTimeOffset object (that is, their ToUniversalTime methods return identical points in time) but whose Offset property is set to offset.

Example

The following example illustrates how to use the ToOffset method to convert a DateTimeOffset object to a DateTimeOffset object with a different offset.


using System;/*from  www.  j a  v a2 s.c  o m*/

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 »
    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