Export (0) Print
Expand All

DateTime.ToUniversalTime Method

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

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

member ToUniversalTime : unit -> DateTime

Return Value

Type: System.DateTime
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.

The Coordinated Universal Time (UTC) is equal to the local time minus the UTC offset. For more information about the UTC offset, see TimeZone.GetUtcOffset. The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.

Important noteImportant

On Windows XP systems, the ToUniversalTime method recognizes only the current adjustment rule when converting from local time to UTC. As a result, conversions for periods before the current adjustment rule came into effect may not accurately reflect the difference between local time and UTC. 

Starting with the .NET Framework version 2.0, the value returned by the ToUniversalTime method is determined by the Kind property of the current DateTime object. The following table describes the possible results.

Kind

Results

Utc

No conversion is performed.

Local

The current DateTime object is converted to UTC.

Unspecified

The current DateTime object is assumed to be a local time, and the conversion is performed as if Kind were Local.

NoteNote

The ToUniversalTime method converts a DateTime value from local time to UTC. To convert the time in a non-local time zone to UTC, use the TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method.

If the date and time instance value is an ambiguous time, this method assumes that it is a standard time. (An ambiguous time is one that can map either to a standard time or to a daylight saving time in the local time zone) If the date and time instance value is an invalid time, this method simply subtracts the local time from the local time zone's UTC offset to return UTC. (An invalid time is one that does not exist because of the application of daylight saving time adjustment rules.)

Notes to Callers

The ToUniversalTime method is sometimes used to convert a local time to UTC. The ToLocalTime method is then called to restore the original local time. However, if the original time represents an invalid time in the local time zone, the two local time values will not be equal. For additional information and an example, see the ToLocalTime method.

On Windows XP systems, the ToUniversalTime method recognizes only the current adjustment rule for the local time zone, which it applies to all dates, including down-level dates (that is, dates that are earlier than the starting date of the current adjustment rule). Applications running on Windows XP that require historically accurate local date and time calculations must work around this behavior by using the FindSystemTimeZoneById method to retrieve a TimeZoneInfo object that corresponds to the local time zone and calling its TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) method.

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. The first two method calls apply the current time zone adjustment rule (which went into effect in 2007) to a date in 2006. The current adjustment rule provides for the transition to daylight saving time on the second Sunday of March; the previous rule, which was in effect in 2006, provided for the transition to daylight saving time to occur on the first Sunday of April. Only the third method call accurately performs this historical date and time conversion.

No code example is currently available or this language may not be supported.

The following example demonstrates the ToUniversalTime method.

No code example is currently available or this language may not be supported.

The following example uses the SpecifyKind method to demonstrate how the Kind property influences the ToLocalTime and ToUniversalTime conversion methods.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft