Jump to content

Is there a way to quick-insert the current date/time in a specific format/structure?


psb01

Recommended Posts

Hi, I have seen several posts about inserting date/time and linking it to a hotkey trigger or short text.

However these all seem to be about using the system date/time information and formats - where we have little control over the date/time elements and their placement in the date/time string we want.

 

As someone who doesn't like the space character in my filenames I wish to be able to quick-enter a text string (mostly in the Finder Save dialog) of the current date/time in the specific format of...

 

YYYY-MM-DD_HH-NN-SS_

 

Where...

 

YYYY - current year in 4 digits.

MM = current month in 2 digits

DD = current date in 2 digits

HH = current hour in 24hour format, 2 digits

NN = current minutes of the hour in 2 digits

SS = current seconds of the minute in 2 digits

 

Also note the structure of the date/time string with the hyphens & underscore characters in place as separators.

 

Is there a way to have this level of control in Alfred and assign it to a shortcut key for easy insertion into any app/field as a plan text string?

 

In Windows I'm sure it can be done with AutoHotKey, but there doesn't seem to be an equivalent application for Macs with this level of control over the date/time elements within some sort of hot-key/text-snippet mechanism.

 

Thank you.

 

Link to comment

This piece of AppleScript will return today's date and the current time in the format you require:

 

set theDateString to my createDate((current date))
set theTimeString to my createTime((current date))
set theResult to theDateString & "_" & theTimeString & "_"

return theResult

on createDate(theDate) -- Process Date
    return (year of theDate as string) & "-" & my zeropad(month of theDate as integer) & "-" & my zeropad(day of theDate)
end createDate

on createTime(theDate) --Process Time
    return (my zeropad(hours of theDate) & "-" & my zeropad(minutes of theDate) & "-" & my zeropad(seconds of theDate))
end createTime

on zeropad(num) -- Add prefixing zero
    if num ≥ 10 then return (num as string)
    return ("0" & num as string)
end zeropad

 

You could link a hotkey (set not to require an argument) to a Run Script action, containing that piece of AppleScript. Link that to a Copy to Clipboard action. In the latter action don't set the action to copy to the frontmost app. Then, when saving a file, all you need to do is to press the hotkey and then ⌘V to replace the suggested file name with the date and time in the relevant format. It all sounds more complicated that it actually is in practice. I've tried it and it works well.

 

Edit: apologies: had reversed day and month but have now corrected script to give the order you want.

 

Stephen

Edited by Stephen_C
Correction to script
Link to comment

@psb01 Vitor is, of course, right (he always is! 😀). To save you messing around with the dynamic placeholder and, out of interest because I've not before used it, this should work if you save it as an Alfred snippet:

 

{isodate: yyyy-MM-dd}_{isotime: HH-mm-ss}_

 

That's less clumsy than my method because you can simply trigger the snippet in a save dialog by typing the trigger over the highlighted word Untitled and that word will then be replaced by the snippet.

 

Stephen

Link to comment
  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...