Welcome to the fracta.net forum!

Share your coding ideas or ask questions.

Question Excel VBA check GMT Offset and Daylight Saving Time/ Standard Time from System Time Zone Information

More
12 years 11 months ago #9 by roller
I found a need to display in my Excel book whether it is summer daylight time or the standard time, and I knew the operating system has this information stored somewhere. Bit a research and I came up with the below code.

The function TZ will return a string with either "Daylight Time" or "Standard Time" as a value


Private Type SYSTEMTIME
Year As Integer
Month As Integer
DayOfWeek As Integer
Day As Integer
Hour As Integer
Minute As Integer
Second As Integer
Milliseconds As Integer
End Type

Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(0 To 63) As Byte
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(0 To 63) As Byte
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type



Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Function TZ() As String
Dim tzi As TIME_ZONE_INFORMATION
lngTZID = GetTimeZoneInformation(tzi)
MsgBox -tzi.Bias / 60 & " GMT Offset" ' this message box will display GMT offset
MsgBox tzi.StandardName ' shows the time zone name

If lngTZID = 2 Then
TZ = "Daylight Time"
Else
TZ = "Standard Time"
End If



End Function


to test run this macro:


sub test ()
msgbox TZ
end sub

Please Log in or Create an account to join the conversation.

Time to create page: 0.729 seconds
Powered by Kunena Forum