Search code examples
user-interfacetimetimerautohotkeyclock

AHK Multiple clocks showing time from different time zones


Good day,

I'm trying to create a script for AutoHotKey (AHK) which would show in a GUI multiple digital clocks (with different time zones). I already created a simple script with 3 different clocks and I'm trying to set the different time zones manually by adding or deducting the hours from my current local time. It almost works, but the third clock resets to my current time after the refresh time (500 ms), so it shows the correct time only for a tiny bit.

GUI:

enter image description here

Script:

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

Gui, font, bold
Gui, add, text, center w90 r1 vdisplay,
Gui, add, text, center w90 r1 vdisplayUTC,
Gui, add, text, center w90 r1 vdisplayadd4,
Gui, show, autosize, Clock

Gosub, currentTime
SetTimer, currentTime, 500
Return

currentTime:
;Clock1
GuiControl,, display, %A_Hour%:%A_Min%
;Clock2
FormatTime, UTC, %A_NowUTC%, HH:mm
GuiControl,, displayutc, %UTC%
;Clock3
EnvAdd, add4, +4, hours
FormatTime, add4, %add4%, HH:mm
GuiControl,, displayadd4, %add4%
Return

GuiEscape:
GuiClose:
ExitApp

What could be the solution?

Ideally I want to add a 4th, 5th and more clocks with the same script.


Solution

  • You need to empty the variable "add4" in each cycle to get its new value:

    #NoEnv
    SendMode Input
    SetWorkingDir %A_ScriptDir%
    
    Gui, font, bold
    Gui, add, text, center w90 r1 vdisplay,
    Gui, add, text, center w90 r1 vdisplayUTC,
    Gui, add, text, center w90 r1 vdisplayadd4,
    Gui, show, autosize, Clock
    
    Gosub, currentTime
    SetTimer, currentTime, 500
    Return
    
    currentTime:
    ;Clock1
    GuiControl,, display, %A_Hour%:%A_Min%
    ;Clock2
    FormatTime, UTC, %A_NowUTC%, HH:mm
    GuiControl,, displayutc, %UTC%
    ;Clock3
    EnvAdd, add4, +4, hours
    FormatTime, add4, %add4%, HH:mm
    GuiControl,, displayadd4, %add4%
    add4 := "" ; empty the variable for next use
    Return
    
    GuiEscape:
    GuiClose:
    ExitApp