How to Quickly Insert the Current Date or Time Using AutoHotkey

Microsoft Windows only. No install required..

If you find this article useful, check out my book Practical AutoHotkey: How to get faster at work with text expansion and automation. The examples and a brief AutoHotkey reference section are available on this website for free.

It’s hard to find a canonical, correct, and up-to-date answer to the question: how do I make AutoHotkey insert the current date/time? Most solutions seem too complicated, possibly too outdated, or both. I built these simple examples to use a minimum of code and maximum of speed.

Try this one instead:

::td::
SendInput %A_YYYY%-%A_MM%-%A_DD%
return

And you’re done!

Now when you type td (followed by an EndChar, eg. space) you’ll end up with something like ‘2021-04-28’. (If you need help editing your script, read more here..)

If you want the time, add the A_Hour and A_Min variables to your string:

::td::
SendInput %A_YYYY%-%A_MM%-%A_DD% %A_Hour%:%A_Min%
return

Unfortunately, if you want AM/PM type time (eg. 2:21 pm, instead of 14:21) you’ll have to use FormatTime, described below. Use the “hh:mm tt” code instead of “HH:mm” in the final example.

American and European Date Formats

Now, maybe you don’t want to use that format and that’s fine. You can simply swap the order around, depending on what you like or where you are in the world.

American date format:

::tdamerica::
SendInput %A_MM%-%A_DD%-%A_YYYY%
return

or European/most of the world date format

::tdeurope::
SendInput %A_DD%-%A_MM%-%A_YYYY%
return

However

Really though, all of us should use the international date format that is clear and works for everyone, as XKCD helpfully explains:

XKCD comic explaining the existance of ISO 8601, which specifies the correct way to write numeric dates -- that is year dash month dash day, all numeric.

The ‘proper’ format was listed in the first example in this article:

::td::
SendInput %A_YYYY%-%A_MM%-%A_DD%
return

More Options for Formatting Dates and Times

The FormatTime command has a lot more options for getting fancy. Here’s the basic usage, in the context of a hotstring like we’re using here.:

::fmtest::
FormatTime, now,, yyyy/M/d HH:mm
SendInput %now%
return

References

If you want to see the full date options available to you, check out the date variables that come with AutoHotkey.

Since the goal here was to write the clearest, shortest possible way to write a date in AutoHotkey, why didn’t I use ‘Send’ instead of ‘SendInput’? In short, because SendInput is faster and when you’re using a program that types for you, you want it as fast as possible

If you found this article useful, check out my book Practical AutoHotkey: How to get faster at work with text expansion and automation. The examples and a brief AutoHotkey reference section are available on this website for free.

You may also like: