Skip to content

Latest commit

 

History

History
60 lines (39 loc) · 4.95 KB

use-time-zones-in-arithmetic.md

File metadata and controls

60 lines (39 loc) · 4.95 KB
description title ms.date dev_langs helpviewer_keywords ms.topic
Learn more about: How to: Use time zones in date and time arithmetic
How to: Use time zones in date and time arithmetic
04/10/2017
csharp
vb
time zones [.NET], arithmetic operations
arithmetic operations [.NET], dates and times
dates [.NET], adding and subtracting
how-to

How to: Use time zones in date and time arithmetic

Ordinarily, when you perform date and time arithmetic using xref:System.DateTime or xref:System.DateTimeOffset values, the result does not reflect any time zone adjustment rules. This is true even when the time zone of the date and time value is clearly identifiable (for example, when the xref:System.DateTime.Kind%2A property is set to xref:System.DateTimeKind.Local). This topic shows how to perform arithmetic operations on date and time values that belong to a particular time zone. The results of the arithmetic operations will reflect the time zone's adjustment rules.

To apply adjustment rules to date and time arithmetic

  1. Implement some method of closely coupling a date and time value with the time zone to which it belongs. For example, declare a structure that includes both the date and time value and its time zone. The following example uses this approach to link a xref:System.DateTime value with its time zone.

    [!code-csharpSystem.DateTimeOffset.Conceptual#6] [!code-vbSystem.DateTimeOffset.Conceptual#6]

  2. Convert a time to Coordinated Universal Time (UTC) by calling either the xref:System.TimeZoneInfo.ConvertTimeToUtc%2A method or the xref:System.TimeZoneInfo.ConvertTime%2A method.

  3. Perform the arithmetic operation on the UTC time.

  4. Convert the time from UTC to the original time's associated time zone by calling the xref:System.TimeZoneInfo.ConvertTime%28System.DateTime%2CSystem.TimeZoneInfo%29?displayProperty=nameWithType method.

Example

The following example adds two hours and thirty minutes to March 9, 2008, at 1:30 A.M. Central Standard Time. The time zone's transition to daylight saving time occurs thirty minutes later, at 2:00 A.M. on March 9, 2008. Because the example follows the four steps listed in the previous section, it correctly reports the resulting time as 5:00 A.M. on March 9, 2008.

[!code-csharpSystem.DateTimeOffset.Conceptual#8] [!code-vbSystem.DateTimeOffset.Conceptual#8]

Both xref:System.DateTime and xref:System.DateTimeOffset values are disassociated from any time zone to which they might belong. To perform date and time arithmetic in a way that automatically applies a time zone's adjustment rules, the time zone to which any date and time value belongs must be immediately identifiable. This means that a date and time and its associated time zone must be tightly coupled. There are several ways to do this, which include the following:

  • Assume that all times used in an application belong to a particular time zone. Although appropriate in some cases, this approach offers limited flexibility and possibly limited portability.

  • Define a type that tightly couples a date and time with its associated time zone by including both as fields of the type. This approach is used in the code example, which defines a structure to store the date and time and the time zone in two member fields.

The example illustrates how to perform arithmetic operations on xref:System.DateTime values so that time zone adjustment rules are applied to the result. However, xref:System.DateTimeOffset values can be used just as easily. The following example illustrates how the code in the original example might be adapted to use xref:System.DateTimeOffset instead of xref:System.DateTime values.

[!code-csharpSystem.DateTimeOffset.Conceptual#7] [!code-vbSystem.DateTimeOffset.Conceptual#7]

Note that if this addition is simply performed on the xref:System.DateTimeOffset value without first converting it to UTC, the result reflects the correct point in time but its offset does not reflect that of the designated time zone for that time.

Compiling the code

This example requires:

  • That the xref:System namespace be imported with the using statement (required in C# code).

See also