Skip to content

Commit c40c28d

Browse files
authored
Porting date and time topics from MSDN (dotnet#992)
* Porting date and time topics from MSDN Previously reviewed - closes dotnet#224 * Fixed broken links and TOC title * Fixed lowercase API names * Still trying to fix build warnings * Fixed reference links * Fixed more reference links * Remove colon from how to metadata * Adressed feedback * Fixed title metadata
1 parent ed86741 commit c40c28d

15 files changed

+2904
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
title: "How to: access the predefined UTC and local time zone objects"
3+
description: How to access the predefined UTC and local time zone objects
4+
keywords: .NET, .NET Core
5+
author: stevehoag
6+
manager: wpickett
7+
ms.date: 08/11/2016
8+
ms.topic: article
9+
ms.prod: .net-core
10+
ms.technology: .net-core-technologies
11+
ms.devlang: dotnet
12+
ms.assetid: 13454d47-d957-421b-9ecd-940058b8835e
13+
---
14+
15+
# How to: access the predefined UTC and local time zone objects
16+
17+
The [System.TimeZoneInfo](xref:System.TimeZoneInfo) class provides two properties, `Utc` and `Local`, that give your code access to predefined time zone objects. This topic discusses how to access the `TimeZoneInfo` objects returned by those properties.
18+
19+
## To access the Coordinated Universal Time (UTC) TimeZoneInfo object
20+
21+
1. Use the **static** (**Shared** in Visual Basic) [TimeZoneInfo.Utc](xref:System.TimeZoneInfo.Utc) property to access Coordinated Universal Time.
22+
23+
2. Rather than assigning the [TimeZoneInfo](xref:System.TimeZoneInfo) object returned by the property to an object variable, continue to access Coordinated Universal Time through the [TimeZoneInfo.Utc](xref:System.TimeZoneInfo.Utc) property.
24+
25+
26+
## To access the local time zone
27+
28+
1. Use the **static** (**Shared** in Visual Basic) [TimeZoneInfo.Local](xref:System.TimeZoneInfo.Local) property to access the local system time zone.
29+
30+
2. Rather than assigning the [TimeZoneInfo](xref:System.TimeZoneInfo) object returned by the property to an object variable, continue to access the local time zone through the [TimeZoneInfo.Local](xref:System.TimeZoneInfo.Local) property.
31+
32+
## Example
33+
34+
The following code uses the [TimeZoneInfo.Local](xref:System.TimeZoneInfo.Local) and [TimeZoneInfo.Utc](xref:System.TimeZoneInfo.Utc) properties to convert a time from the U.S. and Canadian Eastern Standard time zone, as well as to display the time zone name to the console.
35+
36+
```csharp
37+
// Create Eastern Standard Time value and TimeZoneInfo object
38+
DateTime estTime = new DateTime(2007, 1, 1, 00, 00, 00);
39+
string timeZoneName = "Eastern Standard Time";
40+
try
41+
{
42+
TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);
43+
44+
// Convert EST to local time
45+
DateTime localTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local);
46+
Console.WriteLine("At {0} {1}, the local time is {2} {3}.",
47+
estTime,
48+
est,
49+
localTime,
50+
TimeZoneInfo.Local.IsDaylightSavingTime(localTime) ?
51+
TimeZoneInfo.Local.DaylightName :
52+
TimeZoneInfo.Local.StandardName);
53+
54+
// Convert EST to UTC
55+
DateTime utcTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc);
56+
Console.WriteLine("At {0} {1}, the time is {2} {3}.",
57+
estTime,
58+
est,
59+
utcTime,
60+
TimeZoneInfo.Utc.StandardName);
61+
}
62+
catch (TimeZoneNotFoundException)
63+
{
64+
Console.WriteLine("The {0} zone cannot be found in the registry.",
65+
timeZoneName);
66+
}
67+
catch (InvalidTimeZoneException)
68+
{
69+
Console.WriteLine("The registry contains invalid data for the {0} zone.",
70+
timeZoneName);
71+
}
72+
```
73+
74+
```vb
75+
' Create Eastern Standard Time value and TimeZoneInfo object
76+
Dim estTime As Date = #01/01/2007 00:00:00#
77+
Dim timeZoneName As String = "Eastern Standard Time"
78+
Try
79+
Dim est As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName)
80+
81+
' Convert EST to local time
82+
Dim localTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local)
83+
Console.WriteLine("At {0} {1}, the local time is {2} {3}.", _
84+
estTime, _
85+
est, _
86+
localTime, _
87+
IIf(TimeZoneInfo.Local.IsDaylightSavingTime(localTime), _
88+
TimeZoneInfo.Local.DaylightName, _
89+
TimeZoneInfo.Local.StandardName))
90+
91+
' Convert EST to UTC
92+
Dim utcTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc)
93+
Console.WriteLine("At {0} {1}, the time is {2} {3}.", _
94+
estTime, _
95+
est, _
96+
utcTime, _
97+
TimeZoneInfo.Utc.StandardName)
98+
Catch e As TimeZoneNotFoundException
99+
Console.WriteLine("The {0} zone cannot be found in the registry.", _
100+
timeZoneName)
101+
Catch e As InvalidTimeZoneException
102+
Console.WriteLine("The registry contains invalid data for the {0} zone.", _
103+
timeZoneName)
104+
End Try
105+
```
106+
107+
You should always access the local time zone through the [TimeZoneInfo.Local](xref:System.TimeZoneInfo.Local) property rather than assigning the local time zone to a [TimeZoneInfo](xref:System.TimeZoneInfo) object variable. Similarly, you should always access Coordinated Universal Time through the [TimeZoneInfo.Utc](xref:System.TimeZoneInfo.Utc) property rather than assigning the UTC zone to a [TimeZoneInfo](xref:System.TimeZoneInfo) object variable. This prevents the [TimeZoneInfo](xref:System.TimeZoneInfo) object variable from being invalidated by an external method.
108+
109+
110+
## See Also
111+
112+
[Dates, times, and time zones](index.md)
113+
114+
[Finding the time zones defined on a local system](finding-the-time-zones-on-local-system.md)

0 commit comments

Comments
 (0)