Date & Time issues

Mr. Wizard

Board Regular
Joined
Sep 28, 2005
Messages
67
I developed a world clock that can determine the time anywhere in the world based on the =NOW() function. This works fine if someone is in my timezone, but it won't be correct in any other zone on the planet. Can I pull in the user setting for the timezone the user's computer is set to and calculate the relative time for all other time zones? If so, how?

My best solution so far is to use a web query to find GMT rather than the user's position. I was trying for something a little more self-contained.

Any suggestions?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The following code returns the time zone.
Code:
Declare Function GetTimeZoneInformation Lib "kernel32.dll" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Type SYSTEMTIME
  wYear As Integer
  wMonth As Integer
  wDayOfWeek As Integer
  wDay As Integer
  wHour As Integer
  wMinute As Integer
  wSecond As Integer
  wMilliseconds As Integer
End Type

Type TIME_ZONE_INFORMATION
  Bias As Long
  StandardName(0 To 31) As Integer
  StandardDate As SYSTEMTIME
  StandardBias As Long
  DaylightName(0 To 31) As Integer
  DaylightDate As SYSTEMTIME
  DaylightBias As Long
End Type



Sub test()

Dim tzi As TIME_ZONE_INFORMATION
Dim retval As Long
Dim tmz As String
Dim c As Long

    retval = GetTimeZoneInformation(tzi)  ' read information on the computer's selected time zone
    
    For c = 0 To 31  ' the array's range is from 0 to 31
        If tzi.StandardName(c) = 0 Then Exit For  ' abort if the terminating null character is reached
        tmz = tmz & Chr(tzi.StandardName(c))  ' convert the ASCII code into a character and display it
    Next c
    MsgBox tmz & vbCrLf & "GMT " & Format(-tzi.Bias, "+0;-0")  & " mins"
End Sub
 
Upvote 0
I cannot thank you all enough.

The web sites explain the methods, and the code provided works and is easily modified.

:-D
 
Upvote 0

Forum statistics

Threads
1,226,240
Messages
6,189,822
Members
453,572
Latest member
mrjohn500

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top