Run if the current time is between two times (v2) Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
steve88
Posts: 11
Joined: 11 Oct 2018, 04:43

Run if the current time is between two times (v2)

29 Mar 2024, 07:23

Hello everyone! I need to run a portion of code only from Monday to Friday and only if the time is between 8 a.m. and 5 p.m. How can I do it? Thanks!
Reading through the documentation got me this far:

Code: Select all

TimeString := FormatTime(,"HH:mm")
if (A_WDay ~= "2|3|4|5|6") { ;if week day is between monday and friday
	If (xxxxxxxxxxxxx) { ;if TimeString is greater than 08:00 and smaller than 17:00
	;my code
	}
}
Thank you all!
steve88
Posts: 11
Joined: 11 Oct 2018, 04:43

Re: Run if the current time is between two times (v2)

29 Mar 2024, 10:33

Thank you, you are right, checking that variable is the best thing when you are in round times, I didn't think about that. How would you do instead if you wanted to run the script, for example, only from 09:15 to 15:45? Thanks!
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: Run if the current time is between two times (v2)

29 Mar 2024, 10:44

The minutes listing is right after that one.
steve88
Posts: 11
Joined: 11 Oct 2018, 04:43

Re: Run if the current time is between two times (v2)

29 Mar 2024, 11:12

Thank you for the information provided. Comparing the hour and minute variables separately does not lead to the required result, even if the checks are done in a combined manner, because in the intermediate hours the minutes may also be less than 15 or greater than 45. An if set as below is therefore incorrect. How would you set the if? Thank you very much!

Code: Select all

if (A_WDay ~= "2|3|4|5|6") && ((A_Hour >= "09" && A_Min >= "15") && (A_Hour <= "15" && A_Min <= "45"))
steve88
Posts: 11
Joined: 11 Oct 2018, 04:43

Re: Run if the current time is between two times (v2)

29 Mar 2024, 12:17

Perhaps you meant to tell me to do it like this?

Code: Select all

if (A_WDay ~= "2|3|4|5|6" && (A_Hour . A_Min >= 915 && A_Hour . A_Min <= 1545))
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: Run if the current time is between two times (v2)  Topic is solved

29 Mar 2024, 13:07

Yeah. An alternative is below.

Code: Select all

#Requires AutoHotkey v2.0
time := SubStr(A_Now, 9, 4)
If time >= 915 and time <= 1145
     MsgBox time, 1
Else MsgBox time, 2
This would ignore seconds and so would include a time of 11:45:22, for example, which occurs after 11:45:00.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: alawsareps and 103 guests