This topic has not yet been rated - Rate this topic

TimeZoneInfo.Local Property

Gets a TimeZoneInfo object that represents the local time zone.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static TimeZoneInfo Local { get; }

Property Value

Type: System.TimeZoneInfo
An object that represents the local time zone.

The local time zone is the time zone on the computer where the code is executing.

Important note Important

You should always access the local time zone through the TimeZoneInfo.Local property rather than assigning the local time zone to a TimeZoneInfo object variable. This prevents the TimeZoneInfo object variable from being invalidated by a call to the ClearCachedData method.

The TimeZoneInfo object returned by the TimeZoneInfo.Local property reflects the setting of the Automatically adjust clock for daylight saving changes checkbox or the Automatically adjust clock for Daylight Saving Time checkbox in the Control Panel Date and Time application for Windows XP and Windows Vista, respectively. If the checkbox is unchecked, the cached copy of the local time zone contains no daylight saving time information. This means that:

This is not true, however, if a reference to the local time zone is retrieved using the FindSystemTimeZoneById method.

The Local property corresponds to the CurrentTimeZone property of the TimeZone class.

Notes to Callers

In converting dates and times, Windows XP recognizes only the current adjustment rule, 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). On Windows XP, to prevent local date and time information provided by the Local object from diverging from the date and time information displayed in the system tray, the TimeZoneInfo object returned by the Local property also applies the current adjustment rule to down-level dates. 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.

The following example provides an illustration for a Windows XP system in the U.S. Pacific Time zone. Because the first three method calls all use the local time zone returned by the Local property, they 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 to occur 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 fourth method call, which uses the FindSystemTimeZoneById method to retrieve the local time zone, accurately performs this historical date and time conversion.


using System;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2006, 3, 21, 2, 0, 0);

      Console.WriteLine(date1.ToUniversalTime());
      Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1));
      Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1, TimeZoneInfo.Local));

      TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");  
      Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(date1, tz));     
   }
}
// The example displays the following output on Windows XP systems:
//       3/21/2006 9:00:00 AM
//       3/21/2006 9:00:00 AM
//       3/21/2006 9:00:00 AM
//       3/21/2006 10:00:00 AM


The following example retrieves a TimeZoneInfo object that represents the local time zone and outputs its display name, standard time name, and daylight saving time name.


TimeZoneInfo localZone = TimeZoneInfo.Local;
Console.WriteLine("Local Time Zone ID: {0}", localZone.Id);
Console.WriteLine("   Display Name is: {0}.", localZone.DisplayName);
Console.WriteLine("   Standard name is: {0}.", localZone.StandardName);
Console.WriteLine("   Daylight saving name is: {0}.", localZone.DaylightName); 


.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Another Sample using PowerShell
<#
.SYNOPSIS
This script displays/Uses TimeZoneInfo properties
.DESCRIPTION
This script displays the use of all the TimeZoneInfo Properties.
.NOTES
File Name : Get-TimeZoneInfoInfo.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo_properties.aspx
.EXAMPLE
PSH [C:\foo]: .Get-TimeZoneInfoInfo.ps1'
Time Zone: (GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London
Time zone is 0 hours 0 minutes later than Coordinated Universal Time.
Current time is 12:22 PM on 8/28/2010 GMT Daylight Time
Timezone id : GMT Standard Time
Supports Daylight Saving : True
Daylight Saving Zone Name: GMT Daylight Time

Time Zone: (GMT+04:30) Kabul
Time zone is 4 hours 30 minutes later than Coordinated Universal Time.
Current time is 12:22 PM on 8/28/2010 Afghanistan Standard Time
Timezone id : Afghanistan Standard Time
Supports Daylight Saving : False
Daylight Saving Zone Name: Afghanistan Daylight Time

Time Zone: UTC
Time zone is 0 hours 0 minutes later than Coordinated Universal Time.
Current time is 12:22 PM on 8/28/2010 UTC
Timezone id : UTC
Supports Daylight Saving : False
Daylight Saving Zone Name: UTC
#>
# Display Information About Time Zones

# Get current date
$Datenow = Get-Date

# Display info re Local Time Zone
$LocalZone = [System.TimeZoneInfo]::Local
$t1 = [system.Math]::Abs($LocalZone.BaseUtcOffset.Hours)
$t2 = [System.Math]::Abs($LocalZone.BaseUtcOffset.Minutes)
$t3 = if ($LocalZone.BaseUtcOffset -ge [System.Timespan]::Zero) {"later"} else {"earlier"}
$t4 = if ($LocalZone.IsdaylightSavingTime($datenow)) {$Localzone.DaylightName} else {$Localzone.Standardname}
# Display information
"Time Zone: {0}" -f $Localzone.Displayname
" Time zone is {0} hours {1} minutes {2} than Coordinated Universal Time." -f $t1,$t2, $t3
" Current time is {0:t} on {0:d} {1}" -f $datenow,$t4
" Timezone id : {0}" -f $LocalZone.Id
" Supports Daylight Saving : {0}" -f $LocalZone.SupportsDaylightSavingTime
" Daylight Saving Zone Name: {0}" -f $Localzone.DaylightName
""

# Get Kabul Time
$Kt = [system.TimeZoneInfo]::GetSystemTimeZones() | ? {$_.id -match "Afghanistan Standard Time"}
$tz = $kt.Displayname
$t1 = [system.Math]::Abs($kt.BaseUtcOffset.Hours)
$t2 = [System.Math]::Abs($kt.BaseUtcOffset.Minutes)
$t3 = if ($kt.BaseUtcOffset -ge [System.timespan]::Zero) {"later"} else {"earlier"}
$t4 = if ($Kt.IsdaylightSavingTime($datenow)) {$Kt.DaylightName} else {$Kt.Standardname}

# Display information
"Time Zone: {0}" -f $Kt.Displayname
" Time zone is {0} hours {1} minutes {2} than Coordinated Universal Time." -f $t1,$t2,$t3
" Current time is {0:t} on {0:d} {1}" -f $datenow,$t4
" Timezone id : {0}" -f $Kt.Id
" Supports Daylight Saving : {0}" -f $Kt.SupportsDaylightSavingTime
" Daylight Saving Zone Name: {0}" -f $Kt.DaylightName
""

# Display Information regarding UTC
$UtcZone = [System.TimeZoneInfo]::UTC
$t1 = [system.Math]::Abs($UtcZone.BaseUtcOffset.Hours)
$t2 = [System.Math]::Abs($UtcZone.BaseUtcOffset.Minutes)
$t3 = if ($UtcZone.BaseUtcOffset -ge [System.Timespan]::Zero) {"later"} else {"earlier"}
$t4 = if ($UtcZone.IsdaylightSavingTime($datenow)) {$Utczone.DaylightName} else {$UtcZone.Standardname}

# Display information
"Time Zone: {0}" -f $UtcZone.Displayname
" Time zone is {0} hours {1} minutes {2} than Coordinated Universal Time." -f $t1,$t2,$t3
" Current time is {0:t} on {0:d} {1}" -f $Datenow,$t4
" Timezone id : {0}" -f $UtcZone.Id
" Supports Daylight Saving : {0}" -f $UtcZone.SupportsDaylightSavingTime
" Daylight Saving Zone Name: {0}" -f $UtcZone.DaylightName

TimeZoneInfo Sample Using PowerShell
<#
.SYNOPSIS
This script manipulates a time using TimeZone info methods.
.DESCRIPTION
This script reimplements an MSDN sample using PowerShell. The script first
creates a DateTime object then it converts it to Universal, UTC, Pacific and 'local' time.
.NOTES
File Name : Convert-Date.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.local.aspx
.EXAMPLE
PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.TimeZoneINfo\Convert-date.ps
Date : 3/21/2006 02:00:00 AM
Local Time zone: GMT Standard Time
In Universal time: 3/21/2006 02:00:00 AM
In UTC : 3/21/2006 02:00:00 AM
In Local TZ: : 3/21/2006 02:00:00 AM
In Pacific TZ : 3/21/2006 10:00:00 AM
#>
# Create date object
$date1 = New-Object System.DateTime 2006, 3, 21, 2, 0, 0

# Display date and local time zone then show that time in other time zones
"Date : {0}" -f $date1
"Local Time zone: {0}" -f ([System.TimeZoneInfo]::Local).id
" In Universal time: {0}" -f $date1.ToUniversalTime()
" In UTC : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1)
" In Local TZ: : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1, ([System.TimeZoneInfo]::Local))
$tz = [System.TimeZoneInfo]::FindSystemTimeZoneById("Pacific Standard Time");
" In Pacific TZ : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1, $tz)