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.