FormatTime inside of Loop not working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BPet
Posts: 9
Joined: 22 Oct 2017, 08:50

FormatTime inside of Loop not working

Post by BPet » 13 May 2024, 07:58

Does anyone know why this FormatTime within this loop does not work? The localTime variable increments as expected by 1 hour each pass and shows in the message box, but the TimeofDay variable always shows the result from the very first pass of the loop?

Code: Select all

#Include <JSON>
FileRead, var, C:\Program Files (x86)\Cinemar\Batch Files\openweathermap.txt	; get JSON string
obj := JSON.load(var)		; load string into object
loop, 4
{
	unixTime := obj.hourly[A_Index].dt
	time := 1970
	time += unixTime, s
	utcTime := time

	;diff -= A_NowUTC, h
	time += -3, h
	localTime := time

	FormatTime, TimeofDay, localTime, h tt
	
	msgbox, % "localTime: " . localTime . "`nTime of Day: " . TimeofDay 

}
User avatar
mikeyww
Posts: 27194
Joined: 09 Sep 2014, 18:38

Re: FormatTime inside of Loop not working

Post by mikeyww » 13 May 2024, 08:11

Hello,

AHK commands use literal strings unless noted otherwise. Thus, you are using localtime as a string instead of a variable. This is quickly understood by examining an :arrow: example.

Code: Select all

#Requires AutoHotkey v1.1.33.11
localTime := "20200101101010"
FormatTime t1,   localTime   , h tt
FormatTime t2,   abcdefg     , h tt
FormatTime t3,               , h tt
FormatTime t4,  % A_Now      , h tt
FormatTime t5, 20200101101010, h tt
FormatTime t6, % localTime   , h tt
MsgBox % t1 "`n" t2 "`n" t3 "`n" t4 "`n" t5 "`n" t6
YYYYMMDDHH24MISS
If blank or omitted, it defaults to the current local date and time. Otherwise, specify all or the leading part of a timestamp in the YYYYMMDDHH24MISS format. If the date and/or time portion of the timestamp is invalid -- such as February 29th of a non-leap year -- the date and/or time will be omitted from OutputVar. Although only years between 1601 and 9999 are supported, a formatted time can still be produced for earlier years as long as the time portion is valid.
Source: FormatTime - Syntax & Usage | AutoHotkey v1
BPet
Posts: 9
Joined: 22 Oct 2017, 08:50

Re: FormatTime inside of Loop not working

Post by BPet » 14 May 2024, 16:30

Thanks!
Post Reply

Return to “Ask for Help (v1)”