Forum Discussion
DateTimeZone.switchZone using zone ID not time offset
- 4 years ago
So my problem is I want to get my NZT date time to a UTC datetimeZone so I can calculate duration difference from DateTimeZone.UtcNow()
NZT is utc +12 untill last Sunday in September at 02:00 when it becomes +13 for the period of daylight saving.
I cannot just use DateTimeZone.ToUtc([ColumnName]) because I only have a DateTime with no Zone.
I first need convert the NZT datetime to UTC by subtracting the offset duration.
[DateTimeColumnName]- #duration(0,12,0,0)
Then DateTime.AddZone([dateTime], 0) to make it as UTC DateTimeZone
Now I can calculate the duration difference with DateTimeZone.UtcNow()
When I need to report the actual date I use DateTimeZone.Switch([UTCDateTimeZone, 12) to get it to NZT format.
The problem is that the time Offset changes from +12 to +13 on September 26 2021 at 02:00
I want to be able to do the following DateTimeZone.Switch([UTCDateTimeZone, NZT) rather than DateTimeZone.Switch([UTCDateTimeZone,13)
My work around is to create a table with Daylight Saving Periods as below
Year Start End Offset 2021 2021-09-26T02:00 2022-04-03T03:00 13 2021 2021-04-03T03:00 2021-09-26T02:00 12 I then look up the offset using the dateTime I have and use the Offset Value in DateTimeZone.Switch([UTCDateTimeZone, [OffSet])
But this is a time consuming workaround it woult be much easier to just use the timezone id in switch eg DateTimeZone.Switch([UTCDateTimeZone, NZT). I have logged this as an Idea.