9

In a Lisp function, how do I instruct org to automatically insert a timestamp with today's date?

I found the function org-insert-time-stamp, but I don't know how to provide an argument for it that will return today's date.

The documentation for the function says:

(org-insert-time-stamp TIME &optional WITH-HM INACTIVE PRE POST EXTRA)  

...but I don't see where to find documentation about what TIME &optional WITH-HM INACTIVE PRE POST EXTRA means or how to specify these in a function.

And here's the function I'm trying to write:

(defun org-today-heading-and-clock-in ()
  "Insert a new heading with today's date, and then clock in."
  (interactive)
  (org-insert-subheading)
  (org-insert-time-stamp (today))
  (org-clock-in))
itsjeyd
  • 14,586
  • 3
  • 58
  • 87
incandescentman
  • 4,111
  • 16
  • 53

3 Answers3

6

With a bit of guesswork:

(org-insert-time-stamp (current-time))
abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • Thank you. In general, how do I find out what format a function's arguments need to be in, aside from guesswork? – incandescentman Jan 10 '15 at 20:18
  • 1
    Just jump to the definition. For instance, `time` is used by `format-time-string`. And that doc explains more clearly what `time` is. – abo-abo Jan 10 '15 at 20:25
  • 2
    I disagree with @abo-abo. This is a doc bug. The doc string should either refer you to function `format-time-string`, saying that the `TIME` arg is of the same kind, or it should say what the doc string of `format-time-string` says about `TIME`: "*TIME is specified as (HIGH LOW USEC PSEC), as returned by `current-time' or `file-attributes'. The obsolete form (HIGH . LOW) is also still accepted.*" – Drew Jan 10 '15 at 21:02
  • Added to the docstring: See `format-time-string' for the format of TIME – abo-abo Jan 10 '15 at 21:29
  • Use ```(org-time-stamp ARG &optional INACTIVE)``` to give you a pop-up calendar to choose the date that you wish to insert. It is bound to ```C-x i``` by default. – grettke Jan 11 '15 at 00:31
  • 3
    @grettke, it's not `C-x i` by default, it's `C-c .` – nanny Jan 13 '15 at 14:29
  • @nanny Sorry an error occurred between my keyboard and chair when I posted that key binding, indeed it is ```C-c .``` ! – grettke Jan 14 '15 at 14:54
  • There is a shortcut to put in the date with a timestamp: `C-u C-c .` – Steve Oct 05 '18 at 13:24
1

I am using the following two functions with different timestamp formats. The number of digits to be used in year ie 2015 or 15 and hours, minutes, seconds etc can also be specified.

(defun now ()
  "Insert string for the current time formatted like '2:34 PM' or 1507121460"
  (interactive)                 ; permit invocation in minibuffer
  ;;(insert (format-time-string "%D %-I:%M %p")))
  ;;(insert (format-time-string "%02y%02m%02d%02H%02M%02S")))
  (insert (format-time-string "%02y%02m%02d%02H%02M")))

(defun today ()
  "Insert string for today's date nicely formatted in American style,
  e.g. Sunday, September 17, 2000 or standard 17-09-2000."
  (interactive)       ; permit invocation in minibuffer
  ;;(insert (format-time-string "%A, %B %e, %Y")))
  (insert (format-time-string "%d-%m-%y")))
Anusha
  • 419
  • 4
  • 14
0

I get a time stamp using {{{date}}}

Vaibhav
  • 573
  • 3
  • 15