C# DateTime ToUniversalTime

Description

DateTime ToUniversalTime converts the value of the current DateTime object to Coordinated Universal Time (UTC).

Syntax

DateTime.ToUniversalTime has the following syntax.


public DateTime ToUniversalTime()

Returns

DateTime.ToUniversalTime method returns An object whose Kind property is Utc, and whose value is the UTC equivalent to the value of the current DateTime object, or MaxValue if the converted value is too large to be represented by a DateTime object, or MinValue if the converted value is too small to be represented by a DateTime object.

Example

The following example illustrates the difference between the ToUniversalTime and TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) methods on a Windows XP system in the U.S. Pacific Time zone.


//from  ww w.jav  a2 s  .  c o  m
using System;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2014, 3, 21, 2, 0, 0);

      Console.WriteLine(date1.ToUniversalTime());
      Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1));

      TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");  
      Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1, tz));     
   }
}

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