Page 1 of 1

Date string not updating.

Posted: 28 Sep 2018, 05:44
by pteronaut
I am using a hotkey to post a shortdate. However I am having to reload the script every date in order ensure that it pastes the current date.
Is there anything that I'm missing?

Code: Select all

 #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir, %A_ScriptDir%  ; Ensures a consistent starting directory.
SetTitleMatchMode, 1


FormatTime, date,, ShortDate
^+D:: send, %date%
+^S:: send, My Name`t%date%

Re: Date string not updating.

Posted: 28 Sep 2018, 05:49
by Guest
One way

Code: Select all

#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir, %A_ScriptDir%  ; Ensures a consistent starting directory.
SetTitleMatchMode, 1

^+d:: Send, % CurrentDate()
+^s:: Send, % "My Name`t" CurrentDate()

CurrentDate()
    {
     FormatTime, date,, ShortDate
     Return date
    }

Re: Date string not updating.

Posted: 28 Sep 2018, 07:28
by SL5

Code: Select all


^+D:: 
FormatTime, date,, ShortDate
send, %date%
return
+^S:: 
FormatTime, date,, ShortDate
send, My Name`t%date%
return

Re: Date string not updating.

Posted: 28 Sep 2018, 07:29
by MannyKSoSo
If that is failing you could try this

Code: Select all

FormatTime, Date, A_Now, ShortDate

Re: Date string not updating.

Posted: 28 Sep 2018, 07:51
by jeeswg
If you want a one-liner, you can backport the AHK v2 FormatTime function. Cheers.

Code: Select all

^+d:: SendInput, % FormatTime(, "ShortDate")
^+s:: SendInput, % "My Name`t" FormatTime(, "ShortDate")

;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689
FormatTime(YYYYMMDDHH24MISS:="", Format:="")
{
    local OutputVar
    FormatTime OutputVar, %YYYYMMDDHH24MISS%, %Format%
    return OutputVar
}

Re: Date string not updating.

Posted: 28 Sep 2018, 08:38
by pteronaut
Thanks folks.

I'm going to try them in order, it'll be Monday before I know if the 1st one works, and Thursday at the latest to try all the solutions.

Re: Date string not updating.

Posted: 28 Sep 2018, 09:09
by Noesis
Hi pteronaut, the issue you were experiencing is due to location of the "FormatTime" statement in your original code. That statement sets the variable "date", however since the FormatTime statment is located in what the docs refer to as the autexecute section (prior to the first hotkey), this section, and the statements it contain only get evaluated when the script starts, and hence the date variable is never updated again after the script originally runs.

You'll notice in all the solutions given by those above, this statement has been moved in a way that forces the date to be re-evaluated each time a hotkey is used so they all should work, it's just about which solution you prefer. SL5's is probably easiest for you to look at and understand the difference of what is going on, the others are using a function with an inline function call in place of the variable, and is IMO a more elegant way to do it, but figured I'd just add this explanation of what the differences are to help you understand why you experienced what you did with the original code, since it seemed from your post above that you weren't really sure why these would work differently to the original code.