C# DateTimeOffset ToUniversalTime

Description

DateTimeOffset ToUniversalTime converts the current DateTimeOffset object to a DateTimeOffset value that represents the Coordinated Universal Time (UTC).

Syntax

DateTimeOffset.ToUniversalTime has the following syntax.


public DateTimeOffset ToUniversalTime()

Returns

DateTimeOffset.ToUniversalTime method returns An object that represents the date and time of the current DateTimeOffset object converted to Coordinated Universal Time (UTC).

Example

The following example calls the ToUniversalTime method to convert a local time and several other times to Coordinated Universal Time (UTC).


/*  w w w  .j  av  a2s  .c o  m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    DateTimeOffset localTime, otherTime, universalTime;
    
    
    localTime = new DateTimeOffset(new DateTime(2014, 6, 15, 12, 0, 0));
    Console.WriteLine("Local time: {0}", localTime);
    Console.WriteLine();
    
    // Convert local time to offset 0 and assign to otherTime
    otherTime = localTime.ToOffset(TimeSpan.Zero);
    Console.WriteLine("Other time: {0}", otherTime);
    Console.WriteLine("{0} = {1}: {2}", 
                      localTime, otherTime, 
                      localTime.Equals(otherTime));
    Console.WriteLine("{0} exactly equals {1}: {2}", 
                      localTime, otherTime, 
                      localTime.EqualsExact(otherTime));
    

  }
}
    

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