Export (0) Print
Expand All

TimeZone.ToUniversalTime Method

Returns the Coordinated Universal Time (UTC) that corresponds to a specified time.

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

public virtual DateTime ToUniversalTime (
	DateTime time
)
public DateTime ToUniversalTime (
	DateTime time
)
public function ToUniversalTime (
	time : DateTime
) : DateTime
Not applicable.

Parameters

time

A date and time.

Return Value

A DateTime object whose value is the Coordinated Universal Time (UTC) that corresponds to time.

If the local time zone observes daylight saving time, ToUniversalTime applies the current adjustment rule to the time parameter when performing the conversion.

NoteNote:

The ToUniversalTime method recognizes only the current daylight saving time adjustment rule for the local time zone. As a result, it is guaranteed to accurately return the Coordinated Universal Time (UTC) corresponding to a particular local time only during the period in which the latest adjustment rule is in effect. It may return inaccurate results if time is a historic date and time value that was subject to a previous adjustment rule.

If the time parameter is an ambiguous time, the 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 time is an invalid time, the 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.)

Because time is interpreted in relation to the current time zone on the current system, the date and time returned by this method can differ if an application is run on different computers or on the same computer with different time zones. For cases in which a date and time value must represent a single, unambiguous point in time, use a DateTimeOffset value to represent the local time.

The ToUniversalTime method corresponds to the System.TimeZoneInfo.ConvertTimeToUtc(System.DateTime) method with a DateTime parameter whose Kind property does not equal DateTimeKind.Utc.

The following example uses the ToUniversalTime method to return the Coordinated Universal Time (UTC) times that correspond to selected local times.

// Example of the TimeZone.IsDaylightSavingTime( DateTime ),
// TimeZone.IsDaylightSavingTime( DateTime, DaylightTime ), and 
// TimeZone.ToUniversalTime( DateTime ) methods.
using System;
using System.Globalization;

class DaylightTimeDemo
{
    static void Main( )
    {
        const string headFmt = "{0,-22}{1,-10}{2,-10}{3,-10}{4}";

        // Get the local time zone and a base local time.
        TimeZone localZone = TimeZone.CurrentTimeZone;
        DateTime localTime = new DateTime( 2001, 1, 1 );

        Console.WriteLine( "This example of \n" +
            "   TimeZone.IsDaylightSavingTime( DateTime ), \n" +
            "   TimeZone.IsDaylightSavingTime( " +
            "DateTime, DaylightTime ), and \n" +
            "   TimeZone.ToUniversalTime( DateTime )\n" +
            "generates the following output, which varies " +
            "depending on the \ntime zone in which it is run.\n" );
        Console.WriteLine( "The example creates several local " +
            "times and the corresponding \nCoordinated Universal " +
            "Times (UTC) and shows if they occur in \ndaylight " +
            "saving time (DST), both for specified years " +
            "and for \nany year.\n" );
        Console.WriteLine( "Local time: {0}\n", 
            localZone.StandardName );

        Console.WriteLine( headFmt, "Local Date and Time", "2001 DST?", 
            "2002 DST?", "Any DST?", "Corresponding UTC" );
        Console.WriteLine( headFmt, "-------------------", "---------", 
            "---------", "--------", "-----------------" );

        // Create DaylightTime objects for specific years.
        DaylightTime daylight2001 = 
            localZone.GetDaylightChanges( 2001 );
        DaylightTime daylight2002 = 
            localZone.GetDaylightChanges( 2002 );

        // Generate several local times.
        for( int loopX = 0; loopX <= 10; loopX++ )
        {
            // Calculate the corresponding UTC.
            DateTime utcTime = 
                localZone.ToUniversalTime( localTime );

            // Show if dates and times occur in daylight saving 
            // time, for specified years and for any year.
            Console.WriteLine( "{0,-22:yyyy-MM-dd HH:mm}" +
                "{1,-10}{2,-10}{3,-10}{4:yyyy-MM-dd HH:mm}", 
                localTime, 
                TimeZone.IsDaylightSavingTime( 
                    localTime, daylight2001 ), 
                TimeZone.IsDaylightSavingTime( 
                    localTime, daylight2002 ), 
                localZone.IsDaylightSavingTime( localTime ), 
                utcTime );
            
            // Advance to another local time.
            localTime = localTime.AddDays( 109.1 );
        }
    } 
} 

/*
This example of
   TimeZone.IsDaylightSavingTime( DateTime ),
   TimeZone.IsDaylightSavingTime( DateTime, DaylightTime ), and
   TimeZone.ToUniversalTime( DateTime )
generates the following output, which varies depending on the
time zone in which it is run.

The example creates several local times and the corresponding
Coordinated Universal Times (UTC) and shows if they occur in
daylight saving time (DST), both for specified years and for
any year.

Local time: Pacific Standard Time

Local Date and Time   2001 DST? 2002 DST? Any DST?  Corresponding UTC
-------------------   --------- --------- --------  -----------------
2001-01-01 00:00      False     False     False     2001-01-01 08:00
2001-04-20 02:24      True      False     True      2001-04-20 09:24
2001-08-07 04:48      True      False     True      2001-08-07 11:48
2001-11-24 07:12      False     False     False     2001-11-24 15:12
2002-03-13 09:36      False     False     False     2002-03-13 17:36
2002-06-30 12:00      False     True      True      2002-06-30 19:00
2002-10-17 14:24      False     True      True      2002-10-17 21:24
2003-02-03 16:48      False     False     False     2003-02-04 00:48
2003-05-23 19:12      False     False     True      2003-05-24 02:12
2003-09-09 21:36      False     False     True      2003-09-10 04:36
2003-12-28 00:00      False     False     False     2003-12-28 08:00
*/

// Example of the TimeZone.IsDaylightSavingTime( DateTime ),
// TimeZone.IsDaylightSavingTime( DateTime, DaylightTime ), and 
// TimeZone.ToUniversalTime( DateTime ) methods.
import System.*;
import System.Globalization.*;

class DaylightTimeDemo
{
    public static void main(String[] args)
    {
        final String headFmt = "{0,-22}{1,-10}{2,-10}{3,-10}{4}";

        // Get the local time zone and a base local time.
        TimeZone localZone = TimeZone.get_CurrentTimeZone();
        DateTime localTime = new DateTime(2001, 1, 1);

        Console.WriteLine(("This example of \n"
            + "TimeZone.IsDaylightSavingTime( DateTime ), \n" 
            +  "TimeZone.IsDaylightSavingTime( " 
            + "DateTime, DaylightTime ), and \n" 
            +     "   TimeZone.ToUniversalTime( DateTime )\n" 
            +     "generates the following output, which varies " 
            + "depending on the \ntime zone in which it is run.\n"));
        Console.WriteLine(("The example creates several local " 
            + "times and the corresponding \nCoordinated Universal " 
            + "Times (UTC) and shows if they occur in \ndaylight " 
            + "saving time (DST), both for specified years " 
            + "and for \nany year.\n"));
        Console.WriteLine("Local time: {0}\n", localZone.get_StandardName());
        Console.WriteLine(headFmt, new Object[] { 
            "Local Date and Time", "2001 DST?", "2002 DST?", "Any DST?",
            "Corresponding UTC" });
        Console.WriteLine(headFmt, new Object[] { "-------------------",
            "---------", "---------", "--------", "-----------------" });

        // Create DaylightTime objects for specific years.
        DaylightTime daylight2001 = localZone.GetDaylightChanges(2001);
        DaylightTime daylight2002 = localZone.GetDaylightChanges(2002);

        // Generate several local times.
        for (int loopX = 0; loopX <= 10; loopX++) {
            // Calculate the corresponding UTC.
            DateTime utcTime = localZone.ToUniversalTime(localTime);

            // Show if dates and times occur in daylight saving 
            // time, for specified years and for any year.
            Console.WriteLine("{0,-22:yyyy-MM-dd HH:mm}" 
                + "{1,-10}{2,-10}{3,-10}{4:yyyy-MM-dd HH:mm}", new Object[]
                { localTime, (System.Boolean)TimeZone.IsDaylightSavingTime(
                localTime, daylight2001), (System.Boolean)
                TimeZone.IsDaylightSavingTime(localTime, daylight2002),
                (System.Boolean)localZone.IsDaylightSavingTime
                (localTime), utcTime });

            // Advance to another local time.
            localTime = localTime.AddDays(109.1);
        }
    } //main
} //DaylightTimeDemo

/*
This example of
   TimeZone.IsDaylightSavingTime( DateTime ),
   TimeZone.IsDaylightSavingTime( DateTime, DaylightTime ), and
   TimeZone.ToUniversalTime( DateTime )
generates the following output, which varies depending on the
time zone in which it is run.

The example creates several local times and the corresponding
Coordinated Universal Times (UTC) and shows if they occur in
daylight saving time (DST), both for specified years and for
any year.

Local time: Pacific Standard Time

Local Date and Time   2001 DST? 2002 DST? Any DST?  Corresponding UTC
-------------------   --------- --------- --------  -----------------
2001-01-01 00:00      False     False     False     2001-01-01 08:00
2001-04-20 02:24      True      False     True      2001-04-20 09:24
2001-08-07 04:48      True      False     True      2001-08-07 11:48
2001-11-24 07:12      False     False     False     2001-11-24 15:12
2002-03-13 09:36      False     False     False     2002-03-13 17:36
2002-06-30 12:00      False     True      True      2002-06-30 19:00
2002-10-17 14:24      False     True      True      2002-10-17 21:24
2003-02-03 16:48      False     False     False     2003-02-04 00:48
2003-05-23 19:12      False     False     True      2003-05-24 02:12
2003-09-09 21:36      False     False     True      2003-09-10 04:36
2003-12-28 00:00      False     False     False     2003-12-28 08:00
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft