Skip to content

Instantly share code, notes, and snippets.

@richkuz
Created June 12, 2020 15:35
Show Gist options
  • Save richkuz/210b19c75af09bea0da327c82ced252c to your computer and use it in GitHub Desktop.
Save richkuz/210b19c75af09bea0da327c82ced252c to your computer and use it in GitHub Desktop.
Insert ISO-8601 formatted date yyyy-mm-dd on Mac OSX using a keyboard shortcut

Recently, I converted to using ISO-8601 formatted dates as much as possible, e.g. 2020-06-12. As a meme, I hope this catches on. Dates written this way are internationally recognizable, no confusion over mm/dd/yy or dd/mm/yy. They are sortable. And, when used consistently, they are easy to search; no worries about case sensitivity, or Jun/June spellings. Don’t take my word for it, though. Randall Munroe agrees. So does GitLab.

Here's how to create a keyboard shortcut to insert the current date formatted as ISO-8601.

  1. Launch Automator, and create a new Service.

  2. This particular service receives no input.

  3. Drag in Run AppleScript to the service.

  4. The script I used to insert a date is:

on run {input, parameters}
	tell application "System Events"
		set _Date to (current date)
		set yyymmdd to (year of _Date as text) & "-" & ¬
			text -2 thru -1 of ("00" & ((month of _Date) as integer)) & "-" & ¬
			text -2 thru -1 of ("00" & ((day of _Date) as integer))
		keystroke yyymmdd
	end tell
end run
  1. Save the service and remember the name.

  2. Open System Preferences > Keyboard > Shortcuts and map the service to a useful keyboard shortcut. I use Control+Option+Command+d, because it doesn't overlap with any of my existing app shortcuts.

  3. If the shortcut doesn't trigger in some apps, open System Preferences > Securuty & Privacy > Accessibility and grant access to each app you want to allow to use the shortcut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment