views:

3143

answers:

8

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

+8  A: 

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

CMS
+2  A: 

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
+4  A: 

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
+16  A: 
C-u M-! date
How incredibly intuitive :)
spong
+2  A: 

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
A: 

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.

Fred Concklin
A: 

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
A: 

For an answer similar to ones already posted, along with explanations and more extensions such as automatically opening a file and inserting the current date at the end of it (like a journal), check out Paul Ford's discussion of his emacs utilities.

Brendan Foote