96

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!)

Ray
  • 187,153
  • 97
  • 222
  • 204

12 Answers12

148
C-u M-! date
Ray
  • 187,153
  • 97
  • 222
  • 204
  • 66
    Just for completeness: `M-!` is the keyboard shortcut for the function `shell-command`. So `M-! date` will call the shell command `date`, and show it in the output area (the minibuffer, since the output is short enough to fit). The `C-u` is a prefix argument that makes `M-!` put its output in the current buffer instead. – ShreevatsaR Sep 26 '11 at 05:09
  • 13
    You'll get surprising results with this if you use Windows, "date" being a command to change the system date. The answers that handle this question with elisp don't depend on a Unix, Unix-like or even Linux operating system. – Richard Hoskins Jul 28 '14 at 01:52
49

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

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 3
    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. :) – AssembledGhost Apr 10 '12 at 08:55
  • `"\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. – Jack Feb 15 '13 at 03:32
  • The Emacs Lisp manual [recommends `C-c letter`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html) for user-defined key bindings. So changing `"\C-c\C-d"` to `(kbd "C-c d")` ought to avoid conflicts with major modes. – Student Aug 06 '22 at 15:08
36

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

Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
  • 2
    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. – celwell Aug 19 '14 at 17:21
34

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.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
tangxinfa
  • 1,410
  • 13
  • 12
  • is there a way to format it differently than org-mode date format? – Asalle Jun 21 '20 at 16:43
  • 1
    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))) – tangxinfa Jun 24 '20 at 06:54
15

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.

Marcel Levy
  • 3,407
  • 1
  • 28
  • 39
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)
Ryan McGeary
  • 235,892
  • 13
  • 95
  • 104
3

M-1 M-! date

this causes the shell command you run to be inserted into the buffer you are currently editing rather than a new buffer.

3

The simplest way without shelling out to 'date' is probably:

(insert (current-time-string))

Dino Dini
  • 433
  • 3
  • 6
3

To insert date:

C-c . RET 

For select date Shift left/right/up/down - RET (enter)

2

Thanks, CMS! My variation, for what it's worth -- makes me happy enough:

(defvar bjk-timestamp-format "%Y-%m-%d %H:%M"
  "Format of date to insert with `bjk-timestamp' function
%Y-%m-%d %H:%M will produce something of the form YYYY-MM-DD HH:MM
Do C-h f on `format-time-string' for more info")


(defun bjk-timestamp ()
  "Insert a timestamp at the current point.
Note no attempt to go to beginning of line and no added carriage return.
Uses `bjk-timestamp-format' for formatting the date/time."
       (interactive)
       (insert (format-time-string bjk-timestamp-format (current-time)))
       )

I put this in a file that is called by my .emacs using:

(load "c:/bjk/elisp/bjk-timestamp.el")

which both makes it easier to modify without risking breaking something else in my .emacs, and allowed me an easy entry point into maybe someday actually learning what this Emacs Lisp programming is all about.

P.S. Critiques regarding my n00b technique most welcome.

bjkeefe
  • 71
  • 1
  • 8
0

Here's my take on it.

(defun modi/insert-time-stamp (option)
  "Insert date, time, user name - DWIM.

If the point is NOT in a comment/string, the time stamp is inserted prefixed
with `comment-start' characters.

If the point is IN a comment/string, the time stamp is inserted without the
`comment-start' characters. If the time stamp is not being inserted immediately
after the `comment-start' characters (followed by optional space),
the time stamp is inserted with “--” prefix.

If the buffer is in a major mode where `comment-start' var is nil, no prefix is
added regardless.

Additional control:

        C-u -> Only `comment-start'/`--' prefixes are NOT inserted
    C-u C-u -> Only user name is NOT inserted
C-u C-u C-u -> Both prefix and user name are not inserted."
  (interactive "P")
  (let ((current-date-time-format "%a %b %d %H:%M:%S %Z %Y"))
    ;; Insert a space if there is no space to the left of the current point
    ;; and it's not at the beginning of a line
    (when (and (not (looking-back "^ *"))
               (not (looking-back " ")))
      (insert " "))
    ;; Insert prefix only if `comment-start' is defined for the major mode
    (when (stringp comment-start)
      (if (or (nth 3 (syntax-ppss)) ; string
              (nth 4 (syntax-ppss))) ; comment
          ;; If the point is already in a comment/string
          (progn
            ;; If the point is not immediately after `comment-start' chars
            ;; (followed by optional space)
            (when (and (not (or (equal option '(4)) ; C-u or C-u C-u C-u
                                (equal option '(64))))
                       (not (looking-back (concat comment-start " *")))
                       (not (looking-back "^ *")))
              (insert "--")))
        ;; If the point is NOT in a comment
        (progn
          (when (not (or (equal option '(4)) ; C-u or C-u C-u C-u
                         (equal option '(64))))
            (insert comment-start)))))
    ;; Insert a space if there is no space to the left of the current point
    ;; and it's not at the beginning of a line
    (when (and (not (looking-back "^ *"))
               (not (looking-back " ")))
      (insert " "))
    (insert (format-time-string current-date-time-format (current-time)))
    (when (not (equal option '(16))) ; C-u C-u
      (insert (concat " - " (getenv "USER"))))
    ;; Insert a space after the time stamp if not at the end of the line
    (when (not (looking-at " *$"))
      (insert " "))))

I prefer to bind this to C-c d.

Kaushal Modi
  • 1,258
  • 2
  • 20
  • 43
0

This post just showed up when I wanted an easy way to insert current date and time. But none of the answers showed the easiest way to insert time also. Then a bit more searching got me to the answer ...

C-u C-c . to insert active timestamp with current date and time.

C-u C-c ! to insert inactive timestamp with current date and time.

Omit C-u for just date, without time, as Misho posted.

This doesn't have the side effects as eclecticx's answer.

Saj
  • 781
  • 8
  • 7