Emacs: Insert Date Time

Here's a command to insert current date/time.

( require 'ido)

( defun xah-insert-date () "Insert current date time. Insert date in this format: yyyy-mm-dd. If `universal-argument' is called first, prompt for a format to use. If there's text selection, delete it first. URL `http://ergoemacs.org/emacs/elisp_insert-date-time.html' version 2020-09-07" ( interactive ) ( let (( $style ( if current-prefix-arg ( string-to-number ( substring (ido-completing-read "Style:" '( "1 → 2018-04-12 Thursday" "2 → 20180412224611" "3 → 2018-04-12T22:46:11-07:00" "4 → 2018-04-12 22:46:11-07:00" "5 → Thursday, April 12, 2018" "6 → Thu, Apr 12, 2018" "7 → April 12, 2018" "8 → Apr 12, 2018" )) 0 1)) 0 ))) ( when ( use-region-p ) ( delete-region ( region-beginning ) ( region-end ))) ( insert ( cond ((= $style 0) ( format-time-string "%Y-%m-%d" )) ((= $style 1) ( format-time-string "%Y-%m-%d %A" )) ((= $style 2) ( replace-regexp-in-string ":" "" ( format-time-string "%Y%m%d%T" ))) ((= $style 3) ( concat ( format-time-string "%Y-%m-%dT%T" ) ( funcall ( lambda ( $x ) ( format "%s:%s" ( substring $x 0 3) ( substring $x 3 5))) ( format-time-string "%z" ))) ) ((= $style 4) ( concat ( format-time-string "%Y-%m-%d %T" ) ( funcall ( lambda ( $x ) ( format "%s:%s" ( substring $x 0 3) ( substring $x 3 5))) ( format-time-string "%z" ))) ) ((= $style 5) ( format-time-string "%A, %B %d, %Y" ) ) ((= $style 6) ( format-time-string "%a, %b %d, %Y" ) ) ((= $style 7) ( format-time-string "%B %d, %Y" ) ) ((= $style 8) ( format-time-string "%b %d, %Y" ) ) ( t ( format-time-string "%Y-%m-%d" ))))))