Help using a Ternary Operator to Format Time

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Help using a Ternary Operator to Format Time

12 Dec 2018, 11:56

Could someone please assist me in figuring this out? I would like to get the date format correct using a ternary operator. I am not sure what I am doing wrong as this is my first time doing this. Thanks.

Code: Select all

#SingleInstance, force
value := 1
msgbox % FormatTime (value = 1) ? A_Now MM/DD/YYYY : "not the correct value"
return
hymal7
Posts: 66
Joined: 14 Sep 2016, 05:37

Re: Help using a Ternary Operator to Format Time

12 Dec 2018, 12:02

Code: Select all

FormatTime, today,, MM/dd/yyyy
value := 1

if value = 1
{
	msgbox %today%
}
else
{
	msgbox value is not 1
}

return
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Help using a Ternary Operator to Format Time

12 Dec 2018, 12:02

Try this:

Code: Select all

#SingleInstance, force
value := 1
FormatTime result, % (value = 1) ? A_Now : "not the correct value", MM/dd/yyyy
msgbox %result%
return
I hope that helps.
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Help using a Ternary Operator to Format Time

12 Dec 2018, 12:18

wolf_II wrote:
12 Dec 2018, 12:02
Try this:

Code: Select all

#SingleInstance, force
value := 1
FormatTime result, % (value = 1) ? A_Now : "not the correct value", MM/dd/yyyy
msgbox %result%
return
I hope that helps.
You are brilliant!

Just wondering if I can execute more than one command using ternary operators and if I can have more than 2 options. Do you know if this is possible? Thanks.
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Help using a Ternary Operator to Format Time

12 Dec 2018, 12:38

Yes you can, you would just have to build it off another or a combination.
Example

Code: Select all

(value = 1) ? A_Now : ((value = 2) ? "New" : "not the correct value")
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Help using a Ternary Operator to Format Time

12 Dec 2018, 12:43

Thanks for the flowers. :D
The ternary operator can start several commands, but has only two options.
But we can nest them:

Code: Select all

#SingleInstance, force
value := 2
FormatTime result,  % (value = 1) ? A_Now
                    : (value = 2) ? A_YYYY A_MM (A_DD - 1)
                    : "not the correct value"
        , MM/dd/yyyy ; keep the last parameter
msgbox %result%
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 177 guests