Last active
August 7, 2024 18:31
-
-
Save erinnmclaughlin/0c129016ee14139bd711f05f498611f7 to your computer and use it in GitHub Desktop.
Adjusting Dates for Daylight Savings Time in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var easternTime = TimeZoneInfo.FindSystemTimeZoneById("America/New_York"); | |
| for (var month = 1; month <= 12; month++) | |
| { | |
| var date = new DateTime(2024, month, 1); | |
| var utcOffset = easternTime.GetUtcOffset(date); | |
| var timeZoneName = easternTime.IsDaylightSavingTime(date) ? easternTime.DaylightName : easternTime.StandardName; | |
| Console.WriteLine($"On {date}, the UTC offset will be {utcOffset} ({timeZoneName})."); | |
| } | |
| // Output: | |
| // On 1/1/2024 12:00:00 AM, the UTC offset will be -05:00:00 (Eastern Standard Time). | |
| // On 2/1/2024 12:00:00 AM, the UTC offset will be -05:00:00 (Eastern Standard Time). | |
| // On 3/1/2024 12:00:00 AM, the UTC offset will be -05:00:00 (Eastern Standard Time). | |
| // On 4/1/2024 12:00:00 AM, the UTC offset will be -04:00:00 (Eastern Daylight Time). | |
| // On 5/1/2024 12:00:00 AM, the UTC offset will be -04:00:00 (Eastern Daylight Time). | |
| // On 6/1/2024 12:00:00 AM, the UTC offset will be -04:00:00 (Eastern Daylight Time). | |
| // On 7/1/2024 12:00:00 AM, the UTC offset will be -04:00:00 (Eastern Daylight Time). | |
| // On 8/1/2024 12:00:00 AM, the UTC offset will be -04:00:00 (Eastern Daylight Time). | |
| // On 9/1/2024 12:00:00 AM, the UTC offset will be -04:00:00 (Eastern Daylight Time). | |
| // On 10/1/2024 12:00:00 AM, the UTC offset will be -04:00:00 (Eastern Daylight Time). | |
| // On 11/1/2024 12:00:00 AM, the UTC offset will be -04:00:00 (Eastern Daylight Time). | |
| // On 12/1/2024 12:00:00 AM, the UTC offset will be -05:00:00 (Eastern Standard Time). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment