How can I insert current date and time into a file using Emacs?

37,667

Solution 1

Put in your .emacs file:

;; ====================
;; insert date and time

(defvar current-date-time-format "%a %b %d %H:%M:%S %Z %Y"
  "Format of date to insert with `insert-current-date-time' func
See help of `format-time-string' for possible replacements")

(defvar current-time-format "%a %H:%M:%S"
  "Format of date to insert with `insert-current-time' func.
Note the weekly scope of the command's precision.")

(defun insert-current-date-time ()
  "insert the current date and time into current buffer.
Uses `current-date-time-format' for the formatting the date/time."
       (interactive)
       (insert "==========\n")
;       (insert (let () (comment-start)))
       (insert (format-time-string current-date-time-format (current-time)))
       (insert "\n")
       )

(defun insert-current-time ()
  "insert the current time (1-week scope) into the current buffer."
       (interactive)
       (insert (format-time-string current-time-format (current-time)))
       (insert "\n")
       )

(global-set-key "\C-c\C-d" 'insert-current-date-time)
(global-set-key "\C-c\C-t" 'insert-current-time)

Reference

Solution 2

I've used these short snippets:

(defun now ()
  "Insert string for the current time formatted like '2:34 PM'."
  (interactive)                 ; permit invocation in minibuffer
  (insert (format-time-string "%D %-I:%M %p")))

(defun today ()
  "Insert string for today's date nicely formatted in American style,
e.g. Sunday, September 17, 2000."
  (interactive)                 ; permit invocation in minibuffer
  (insert (format-time-string "%A, %B %e, %Y")))

They originally came from journal.el

Solution 3

For insert date:

M-x org-time-stamp

For insert date time:

C-u M-x org-time-stamp

You may bind a global key for this command.

org-mode's method is very user friendly, you can select any date from calendar.

Solution 4

You can install yasnippet, which will let you type "time" and the tab key, and does a whole lot more besides. It just calls current-time-string behind the scenes, so you can control the formatting using format-time-string.

Solution 5

Here's a package I wrote a while ago that does what you're asking for.

http://github.com/rmm5t/insert-time.el/tree/master/insert-time.el

(require 'insert-time)
(define-key global-map [(control c)(d)] 'insert-date-time)
(define-key global-map [(control c)(control v)(d)] 'insert-personal-time-stamp)
Share:
37,667
Ray
Author by

Ray

Writes code and likes it. A lot.

Updated on June 30, 2021

Comments

  • Ray
    Ray almost 3 years

    What commands in Emacs can I use to insert into the text buffer of a file the current date and time?

    (For example, the equivalent in Notepad is simply pressing F5 which is about the only useful feature for Notepad!)

  • AssembledGhost
    AssembledGhost about 12 years
    I use something alike to this, but I change it from C-c C-t to C-x C-t because C-c C-t gets in the way of org-mode's "Mark TODO item" binding. Which is unfortunatly hardwired into muscle memory for me. :)
  • Jack
    Jack about 11 years
    "\C-c\C-d" nothing happens for me. Actually,it delete current line when C-d is typed. But I think that why C-c is not working.
  • Richard Hoskins
    Richard Hoskins almost 10 years
    yasnippet is a resource hog compared to the built-in templates and hippie-expand.
  • celwell
    celwell over 9 years
    thanks. very nice. for anyone who is completely a noob at emacs: Use this by adding it to your .emacs. file and save it, and then move your cursor (point) to the end parenthesis of each function and then M-x M-e for each. Now you can insert using M-x now or M-x today wherever you want.
  • Asalle
    Asalle almost 4 years
    is there a way to format it differently than org-mode date format?
  • tangxinfa
    tangxinfa almost 4 years
    The date time format insert by org-time-stamp is specified by org-time-stamp-formats. ;; insert date with customized format. (let ((org-time-stamp-formats '("%Y-%m-%d" . "%Y-%m-%d %H:%M:%S"))) (org-time-stamp nil)) ;; insert date with customized format. (let ((org-time-stamp-formats '("%Y-%m-%d" . "%Y-%m-%d %H:%M:%S"))) (org-time-stamp '(4)))
  • dat
    dat over 2 years
    C-c . is bound in org mode -- but isn't bound by default by text mode.