Date string not updating.

Get help with using AutoHotkey and its commands and hotkeys
pteronaut
Posts: 6
Joined: 10 Feb 2018, 16:25

Date string not updating.

28 Sep 2018, 05:44

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%
Guest

Re: Date string not updating.

28 Sep 2018, 05:49

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
    }
User avatar
SL5
Posts: 853
Joined: 12 May 2015, 02:10
GitHub: sl5net
Contact:

Re: Date string not updating.

28 Sep 2018, 07:28

Code: Select all


^+D:: 
FormatTime, date,, ShortDate
send, %date%
return
+^S:: 
FormatTime, date,, ShortDate
send, My Name`t%date%
return
MannyKSoSo
Posts: 429
Joined: 28 Apr 2018, 21:59

Re: Date string not updating.

28 Sep 2018, 07:29

If that is failing you could try this

Code: Select all

FormatTime, Date, A_Now, ShortDate
User avatar
jeeswg
Posts: 6727
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Date string not updating.

28 Sep 2018, 07:51

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
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
pteronaut
Posts: 6
Joined: 10 Feb 2018, 16:25

Re: Date string not updating.

28 Sep 2018, 08:38

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.
Noesis
Posts: 299
Joined: 26 Apr 2014, 07:57

Re: Date string not updating.

28 Sep 2018, 09:09

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.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], daiweisc, flyingDman, lmstearn and 41 guests