Вопрос

I found the following code from a website (sorry, I cannot remember the site now for references), however, it gave me the current time zone offset for my computer. So for example, this code:

Dim plusminus As String = If((tz.Hours > 0), "+", "-")
Dim NewTimeZone As String = String.Format("{0}{1:00}:{2:00}", plusminus, Math.Abs(tz.Hours), tz.Minutes)

Gave me '+09:30' as an output. Which was perfect. However since daylight savings has come into affect, the same code now gives me '+10:30' which is in fact incorrect.

Can someone please advsise of a better method for retrieving the offset correctly (both in and out of daylight savings time) and returned in the same format (+09:30)

Thanks in advance

Это было полезно?

Решение

If I understand correctly you want to find the difference between UTC and the current time irrespective of any daylight savings?

If I am correct then the following will do what you want:

Dim tzi As TimeZoneInfo = TimeZoneInfo.Local
Dim offset As TimeSpan = tzi.BaseUtcOffset
Dim plus As String = If(offset.TotalMinutes >= 0, "+", "")
Dim newTimeZone As String = String.Format("{0}{1:00}:{2:00}", plus, offset.Hours, offset.Minutes)

Outputs +00:00 on my machine (my local time is UTC+1 but we are in DST currently)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top