Note that there are some explanatory texts on larger screens.

plurals
  1. POUse TimeZoneInfo class to account for time change in daylight savings time
    primarykey
    data
    text
    <p>I have some outdated code that attempts to account for the change in time caused by daylight savings that looks like this:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { for (int i = 1; i &lt;= 31; i++) { DateTime dt = new DateTime(1960, 3, i, 0, 0, 0); Console.WriteLine(dt.ToUniversalTime()); } Console.WriteLine(); for (int i = 1; i &lt;= 30; i++) { DateTime dt = new DateTime(1960, 4, i, 0, 0, 0); Console.WriteLine(dt.ToUniversalTime()); } Console.ReadKey(); } } } </code></pre> <p>This code iterates through the days in March and April in 1960 and prints the datetime. However, this does not correctly account for the time change in 1960, I believe because the date of the time change was different then. I attempted to fix this using the TimeZoneInfo class. I changed the code to the following:</p> <pre><code>class Program { static void Main(string[] args) { for (int i = 1; i &lt;= 31; i++) { DateTime dt = new DateTime(1960, 3, i, 0, 0, 0); var tz = TimeZoneInfo.Local; var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero); //use timeZoneInfo class to account for dlst offset Console.WriteLine(utcOffset.ToOffset(tz.GetUtcOffset(utcOffset))); } Console.WriteLine(); for (int i = 1; i &lt;= 30; i++) { DateTime dt = new DateTime(1960, 4, i, 0, 0, 0); var tz = TimeZoneInfo.Local; var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero); //use timeZoneInfo class to account for dlst offset Console.WriteLine(utcOffset.ToOffset(tz.GetUtcOffset(utcOffset))); } Console.ReadKey(); } } </code></pre> <p>Unfortunately, this is printing out: <img src="https://i.stack.imgur.com/eZz9C.png" alt="enter image description here"></p> <p>which shows that daylight savings is changing on April 3rd at 4 p.m., while it should be switching over at April 24th at 2:00 a.m. What am I missing to correctly account for daylight savings?</p> <p><strong>EDIT:</strong> My current time zone is eastern.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload