This documentation is archived and is not being maintained.

TimeZoneInfo.ConvertTimeToUtc Method (DateTime)

Converts the current date and time to Coordinated Universal Time (UTC).

Namespace:  System
Assembly:  System.Core (in System.Core.dll)

public static DateTime ConvertTimeToUtc(
	DateTime dateTime
)

Parameters

dateTime
Type: System.DateTime

The date and time to convert.

Return Value

Type: System.DateTime
A DateTime value that represents the Coordinated Universal Time (UTC) that corresponds to the dateTime parameter. The DateTime value's Kind property is always set to DateTimeKind.Utc.

ExceptionCondition
ArgumentException

TimeZoneInfo.Local.IsInvalidDateTime(dateTime) returns true.

The exact conversion performed depends on the value of the Kind property of the dateTime parameter, as the following table shows.

DateTime.Kind property

Conversion

DateTimeKind.Local

Converts from local time to Coordinated Universal Time (UTC).

DateTimeKind.Unspecified

Assumes dateTime is local time and converts from local time to UTC.

DateTimeKind.Utc

Returns dateTime unchanged.

If dateTime corresponds to an ambiguous local time, this method assumes that it is standard local time. If dateTime corresponds to an invalid local time, the method throws an ArgumentException.

NoteNote:

If the current computer's local time zone includes multiple adjustment rules, this overload of the ConvertTimeToUtc method can return results that differ from the TimeZone.ToUniversalTime and DateTime.ToUniversalTime methods

If the UTC equivalent of dateTime is earlier than DateTime.MinValue or later that DateTime.MaxValue, this method returns MinValue or MaxValue, respectively.

The following example illustrates the conversion of time values whose Kind property is DateTimeKind.Utc, DateTimeKind.Local, and DateTimeKind.Unspecified, respectively. It also illustrates the conversion of ambiguous and invalid times.

DateTime datNowLocal = DateTime.Now;
Console.WriteLine("Converting {0}, Kind {1}:", datNowLocal, datNowLocal.Kind);
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNowLocal), TimeZoneInfo.ConvertTimeToUtc(datNowLocal).Kind);
Console.WriteLine();

DateTime datNowUtc = DateTime.UtcNow;
Console.WriteLine("Converting {0}, Kind {1}", datNowUtc, datNowUtc.Kind);
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNowUtc), TimeZoneInfo.ConvertTimeToUtc(datNowUtc).Kind);
Console.WriteLine();

DateTime datNow = new DateTime(2006, 10, 26, 13, 32, 00);
Console.WriteLine("Converting {0}, Kind {1}", datNow, datNow.Kind);
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNow), TimeZoneInfo.ConvertTimeToUtc(datNow).Kind);
Console.WriteLine();

DateTime datAmbiguous = new DateTime(2007, 11, 4, 1, 30, 00);    
Console.WriteLine("Converting {0}, Kind {1}, Ambiguous {2}", datAmbiguous, datAmbiguous.Kind, TimeZoneInfo.Local.IsAmbiguousTime(datAmbiguous));
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datAmbiguous), TimeZoneInfo.ConvertTimeToUtc(datAmbiguous).Kind);
Console.WriteLine();

DateTime datInvalid = new DateTime(2007, 3, 11, 02, 30, 00);    
Console.WriteLine("Converting {0}, Kind {1}, Invalid {2}", datInvalid, datInvalid.Kind, TimeZoneInfo.Local.IsInvalidTime(datInvalid));
try
{
   Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datInvalid), TimeZoneInfo.ConvertTimeToUtc(datInvalid).Kind);
}
catch (ArgumentException e)
{   
   Console.WriteLine("   {0}: Cannot convert {1} to UTC.", e.GetType().Name, datInvalid);
}
Console.WriteLine();

DateTime datNearMax = new DateTime(9999, 12, 31, 22, 00, 00);
Console.WriteLine("Converting {0}, Kind {1}", datNearMax, datNearMax.Kind);
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNearMax), TimeZoneInfo.ConvertTimeToUtc(datNearMax).Kind);
Console.WriteLine();
// 
// This example produces the following output if the local time zone  
// is Pacific Standard Time: 
// 
//    Converting 8/31/2007 2:26:28 PM, Kind Local: 
//       ConvertTimeToUtc: 8/31/2007 9:26:28 PM, Kind Utc 
//     
//    Converting 8/31/2007 9:26:28 PM, Kind Utc 
//       ConvertTimeToUtc: 8/31/2007 9:26:28 PM, Kind Utc 
//     
//    Converting 10/26/2007 1:32:00 PM, Kind Unspecified 
//       ConvertTimeToUtc: 10/26/2007 8:32:00 PM, Kind Utc 
//     
//    Converting 11/4/2007 1:30:00 AM, Kind Unspecified, Ambiguous True 
//       ConvertTimeToUtc: 11/4/2007 9:30:00 AM, Kind Utc 
//     
//    Converting 3/11/2007 2:30:00 AM, Kind Unspecified, Invalid True 
//       ArgumentException: Cannot convert 3/11/2007 2:30:00 AM to UTC. 
//     
//    Converting 12/31/9999 10:00:00 PM, Kind Unspecified 
//       ConvertTimeToUtc: 12/31/9999 11:59:59 PM, Kind Utc 
// 

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5
Show: