Using the DateTime GUI to Enter Both Times and Dates into the AutoHotkey Hotstring Replacement Menus
Last time, I added the MonthCal GUI control to the DateMenus.ahk script which allows the selection of any date for the date formatting pop-up menu. The primary problem with using the MonthCal GUI was that there is no time component in the result. If all you need is the date inserted into a document, then MonthCal works great. However, to get an option for including any time of day, the DateTime GUI control does the trick.
* * *
New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”
* * *
As a convenience for people who want to get this entire series (plus more) in one place, I have now published the e-book Beginning AutoHotkey Hotstrings which is available on the ComputorEdge E-Books site (in three formats—EPUB for mobile devices, MOBI for Kindle, and PDF for printing and computer) and through Amazon. Regardless of whether you’re interested in the book or not, it’s worth your time to peruse some of the blogs linked at “Beginning AutoHotkey Hotstring Techniques” found under the “AutoHotkey Topics and Series” tab in the top menu bar. They just might inspire your next AutoHotkey script.
* * *
See how to get the 201 page* e-book AutoHotkey Tricks FREE! (*According to Amazon calculations)
* * *

The DateTime GUI control is a complete time/date input widget with an optional popdown calendar. (See image at right.) This GUI can be as simple as only displaying editing fields for changing the time by specifying Time as the last parameter in the command line. (With the Time parameter in place, an up/down arrow control is available on the right side of the control for making time changes.)
If the last parameter is LongDate, then the DateTime GUI essentially becomes a MonthCal GUI—except that the current time, although hidden, is always included rather than a constant midnight (12am).
For working with both the time and date, the layout of the DateTime GUI can be formatted in the same manner as the FormatTime command:
Gui, AddDate:Add, DateTime, vDayPick, hh:mm tt, dddd MMMM d, yyyy
Adding this new GUI to the DateMenus.ahk script (available for download at the ComputorEdge AutoHotkey download site) is as simple as adding another conditional for the Hotstring date. This time the closed square bracket (]) is used as the activating character.
Else If A_EndChar = ] { SendInput {Backspace 5} IfWinExist, Select Date Gui AddDate:Destroy Gui AddDate:+AlwaysOnTop Gui, AddDate:Add, DateTime, vDayPick , hh:mm tt, dddd MMMM d, yyyy Gui, AddDate:Add, Button, Default, Submit Gui, AddDate:Show,, Select Date or Time }
(The Gui, AddDate:Add, DateTime line of code in the above snippet has been wrapped with AutoHotkey line continuation for display purposes only. Since the second line starts with a comma, AutoHotkey automatically reads it as part of the previous line.)

When date] (the word “date” followed by the closing square bracket “]”) is typed into any Windows document, the DateTime GUI window AddDate opens in the format shown in the image above. Edit the time/date fields and click the Submit button (hidden under the popdown calendar) and the selected time and date will be added to the Hotstring pop-up menu (shown at right).
This bit of code is almost identical to the conditional implemented last time for the MonthCal control. The script uses all of the same functions and subroutines to complete its work. Other than the line of code adding the DateTime GUI control, the primary difference is the addition of the line Gui AddDate:+AlwaysOnTop which forces the window to stay in view (on top) on the computer screen. This line has been added to deal with occasional issues connected with various windows losing focus.
Tracking the Original Edit Window
I recently upgraded one of my computers to Windows 10. Even though I had included WinActivate in the original DateMenus.ahk script, initially, the script would not work properly. I added a few other window related AutoHotkey commands which got it up and running again. It continued to function after that time—even though I removed the new commands. I finally decided to find a more reliable technique.
Rather than depending upon the editing window being the last window accessed, I switched to saving the original window’s ID, later using that ID to reactivate the window before inserting the selected date. This technique works exceptionally well. Plus, it has the added benefit of allowing me to access an unlimited number of other windows before I make the time/date selection, yet the script always adds that menu date to the appropriate editing field.
The following is the new Hotstring using the WinActive() function to save the window ID:
:B0C:date:: ActWin := WinActive("A") If A_EndChar = - { SendInput {Backspace 5} Date := A_Now List := DateFormats(Date) TextMenuDate(List) } Else If A_EndChar = `; { SendInput {Backspace 5} IfWinExist, Select Date Gui AddDate:Destroy Gui AddDate:+AlwaysOnTop ; visible until selection Gui, AddDate:Add, MonthCal, vDayPick ;r4 w-3 Gui, AddDate:Add, Button, Default, Submit Gui, AddDate:Show,, Select Date } Else If A_EndChar = ] { SendInput {Backspace 5} IfWinExist, Select Date Gui AddDate:Destroy Gui AddDate:+AlwaysOnTop Gui, AddDate:Add, DateTime, vDayPick , hh:mm tt, dddd MMMM d, yyyy Gui, AddDate:Add, Button, Default, Submit Gui, AddDate:Show,, Select Date or Time } Return
The line ActWin := WinActive(“A”) automatically saves the ID of the active window (“A”) to the variable ActWin. This is the first action invoked whenever the date Hotstring is tested with any activating key—even if none of the conditions are met. Now, with this new added feature, the editing windows can even be minimized without getting lost. The window ID (ActWin) is used later in the MenuAction: subroutine to reactivate the window just before inserting the properly formatted time and/or date.
Tip: The WinGet command can be used as a substitute for the WinActive() function:
WinGet, ActWin, ID, A
Note: I’ve added the Gui AddDate:+AlwaysOnTop to keep the GUI in sight until a selection is made. Otherwise, the GUI might be lost (and possibly forgotten) behind other windows while the user is opening various programs to determine the proper time or date.
Ultimately, after the time/date format selection is made from the pop-up menu, the original window is activated in the MenuAction: subroutine:
MenuAction:
WinActivate, ahk_id %ActWin% ;activate original window
If A_ThisMenuItem Is Alpha
{
SendInput %A_ThisMenuItem%{Raw}%A_EndChar%
}
Else
SendInput %A_ThisMenuItem%
Return
The WinActivate command is used with the window ID (ahk_id) to activate the window %ActWin%.
* * *
Find Jack’s AutoHotkey e-Books at ComputorEdgeBooks.com.