Emacs: Insert Date Time 💠

By Xah Lee. Date: . Last updated: .

Here's a command to insert current date time.

emacs date time 2025-04-11 22d36
emacs date time 2025-04-11 22d36
(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 is selection, delete it first.

URL `http://xahlee.info/emacs/emacs/elisp_insert-date-time.html'
Created: 2013-05-10
Version: 2025-04-12"
  (interactive)
  (let (xmenu xstyle)
    (setq
     xmenu
     (list
      (concat "ISO dateâš«" (format-time-string "%Y-%m-%d"))
      (concat "coderâš«" (format-time-string "%Y-%m-%d_%H%M%S"))
      (concat "all digitsâš«" (format-time-string "%Y%m%d%H%M%S"))

      (concat "unix secondsâš«" (number-to-string (car (let ((current-time-list nil)) (current-time)))))

      ;; (concat "ISO fullâš«" (format-time-string "%Y-%m-%dT%T") (funcall (lambda (xx) (format "%s:%s" (substring xx 0 3) (substring xx 3 5))) (format-time-string "%z")))

      (concat "ISO fullâš«" (format-time-string "%FT%T%z"))

      (concat "ISO + weekdayâš«" (format-time-string "%Y-%m-%d %A"))
      (concat "USA + weekdayâš«" (format-time-string "%A, %B %d, %Y"))
      (concat "USA + weekday abbrevâš«" (format-time-string "%a, %b %d, %Y"))
      (concat "USAâš«" (format-time-string "%B %d, %Y"))
      (concat "USA abbrevâš«" (format-time-string "%b %d, %Y"))))
    (setq xstyle
          (if current-prefix-arg
              (let ((completion-ignore-case t))
                (ido-completing-read "Style:" xmenu nil t))
            (car xmenu)))
    (when (region-active-p) (delete-region (region-beginning) (region-end)))
    (insert (nth 1 (split-string xstyle "âš«")))))

emacs lisp, Datetime