TimeZone Class

Definition

Caution

System.TimeZone has been deprecated. Please investigate the use of System.TimeZoneInfo instead.

Caution

System.TimeZone has been deprecated. Investigate the use of System.TimeZoneInfo instead.

Represents a time zone.

[System.Obsolete("System.TimeZone has been deprecated.  Please investigate the use of System.TimeZoneInfo instead.")]
public abstract class TimeZone
[System.Obsolete("System.TimeZone has been deprecated. Investigate the use of System.TimeZoneInfo instead.")]
public abstract class TimeZone
[System.Serializable]
public abstract class TimeZone
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class TimeZone
Inheritance
TimeZone
Attributes

Examples

The following example references and displays selected TimeZone class elements.

// Example of selected TimeZone class elements.
using System;
using System.Globalization;

class TimeZoneDemo
{
    static void Main( )
    {
        const string dataFmt = "{0,-30}{1}";
        const string timeFmt = "{0,-30}{1:yyyy-MM-dd HH:mm}";

        Console.WriteLine(
            "This example of selected TimeZone class " +
            "elements generates the following \n" +
            "output, which varies depending on the " +
            "time zone in which it is run.\n" );

        // Get the local time zone and the current local time and year.
        TimeZone localZone = TimeZone.CurrentTimeZone;
        DateTime currentDate = DateTime.Now;
        int      currentYear = currentDate.Year;

        // Display the names for standard time and daylight saving 
        // time for the local time zone.
        Console.WriteLine( dataFmt, "Standard time name:", 
            localZone.StandardName );
        Console.WriteLine( dataFmt, "Daylight saving time name:", 
            localZone.DaylightName );

        // Display the current date and time and show if they occur 
        // in daylight saving time.
        Console.WriteLine( "\n" + timeFmt, "Current date and time:",
            currentDate );
        Console.WriteLine( dataFmt, "Daylight saving time?", 
            localZone.IsDaylightSavingTime( currentDate ) );

        // Get the current Coordinated Universal Time (UTC) and UTC 
        // offset.
        DateTime currentUTC = 
            localZone.ToUniversalTime( currentDate );
        TimeSpan currentOffset = 
            localZone.GetUtcOffset( currentDate );

        Console.WriteLine( timeFmt, "Coordinated Universal Time:", 
            currentUTC );
        Console.WriteLine( dataFmt, "UTC offset:", currentOffset );

        // Get the DaylightTime object for the current year.
        DaylightTime daylight = 
            localZone.GetDaylightChanges( currentYear );

        // Display the daylight saving time range for the current year.
        Console.WriteLine( 
            "\nDaylight saving time for year {0}:", currentYear );
        Console.WriteLine( "{0:yyyy-MM-dd HH:mm} to " +
            "{1:yyyy-MM-dd HH:mm}, delta: {2}", 
            daylight.Start, daylight.End, daylight.Delta );
    } 
} 

/*
This example of selected TimeZone class elements generates the following
output, which varies depending on the time zone in which it is run.

Standard time name:           Pacific Standard Time
Daylight saving time name:    Pacific Daylight Time

Current date and time:        2006-01-06 16:47
Daylight saving time?         False
Coordinated Universal Time:   2006-01-07 00:47
UTC offset:                   -08:00:00

Daylight saving time for year 2006:
2006-04-02 02:00 to 2006-10-29 02:00, delta: 01:00:00
*/

Remarks

A time zone is a geographical region in which the same standard time is used.

重要

Whenever possible, use the TimeZoneInfo class instead of the TimeZone class.

You can use the TimeZone class to retrieve information about the current time zone, and to convert times from local time to Coordinated Universal Time (UTC) or vice versa. However, you cannot use the TimeZone class to represent time zones other than the local zone or to handle date and time conversions from one time zone to another. For this purpose, use the TimeZoneInfo class. You can use this class to retrieve information on any time zone defined on the local system, to create custom time zones, and to convert times from one time zone to another.

The TimeZone class supports only a single daylight saving time adjustment rule for the local time zone. As a result, the TimeZone class can accurately report daylight saving time information or convert between UTC and local time only for the period in which the latest adjustment rule is in effect. In contrast, the TimeZoneInfo class supports multiple adjustment rules, which makes it possible to work with historic time zone data.

Notes to Implementers

In addition to providing implementations for its abstract members (those marked MustOverride in Visual Basic), it is important that classes derived from TimeZone override the default behavior of the ToLocalTime(DateTime) method. This is because the default behavior of ToLocalTime(DateTime) in the .NET Framework version 2.0 does not depend on a call to GetUtcOffset(DateTime), as it did in the .NET Framework versions 1.0 and 1.1. For details, see the ToLocalTime(DateTime) method.

Constructors

TimeZone()

Initializes a new instance of the TimeZone class.

Properties

CurrentTimeZone

Gets the time zone of the current computer.

DaylightName

Gets the daylight saving time zone name.

StandardName

Gets the standard time zone name.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetDaylightChanges(Int32)

Returns the daylight saving time period for a particular year.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
GetUtcOffset(DateTime)

Returns the Coordinated Universal Time (UTC) offset for the specified local time.

IsDaylightSavingTime(DateTime, DaylightTime)

Returns a value indicating whether the specified date and time is within the specified daylight saving time period.

IsDaylightSavingTime(DateTime)

Returns a value indicating whether the specified date and time is within a daylight saving time period.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToLocalTime(DateTime)

Returns the local time that corresponds to a specified date and time value.

ToString()

Returns a string that represents the current object.

(Inherited from Object)
ToUniversalTime(DateTime)

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

Applies to

产品 版本 (已过时)
.NET (Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard (2.0, 2.1)

See also