It's easy to do date arithmetic using org-mode table formulas. For example, if I enter this table:

| [2020-01-01] | |
| [2020-02-01] | |
| [2020-03-01] | |
#+TBLFM: $2=$1+1

...and then do C-c C-c on the formula, the table updates column 2, adding one day to the dates in column 1.

Can this be done using a Lisp formula instead of a calc formula? If so, how?

I see in the org-mode manual that Lisp formulas support various trailing flags to interpret the table cells in different formats, but not datestamps. I experimented with calc-eval, but calc datestamps differ in format from org datestamps. It appears there's some invisible plumbing that connects the two.

  • 1
    Example: (org-format-time-string "[%F]" (org-time-from-absolute (1+ (org-time-string-to-absolute "[2020-01-01]"))))
    – Tobias
    Mar 25 '20 at 20:20
  • The basic idea behind @Tobias's example is that entries in an Org mode table are passed to a lisp expression as strings: the rest is a matter of converting them to a data type that you can use for calculation, do the calculation and then convert the result back to a string in order to store it in the table.
    – NickD
    Mar 25 '20 at 23:29

You can use org-table-eval-formula to do the calculation in-place of the field and then retrieve the field value which the lisp form must return with org-table-get-field. In that way you can evaluate the calc formula within your lisp form.

In lisp formulae $1 delivers a string "[2020-01-01]" for the date [2020-01-01]. Therefore the somewhat awkward construction of the calc formula (concat $1 "+1").

See the following example:

| [2020-01-01] |  |
| [2020-02-01] |  |
| [2020-03-01] |  |
#+TBLFM: $2='(progn (org-table-eval-formula nil (concat $1 "+1") t t t t) (org-table-get-field))

In this special case where you really only want date calculation you can also use the following Elisp formula instead:

| [2020-01-01] | [2020-01-02] |
| [2020-02-01] | [2020-02-02] |
| [2020-03-01] | [2020-03-02] |
#+TBLFM: $2='(org-format-time-string "[%F]" (org-time-from-absolute (1+ (org-time-string-to-absolute $1))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.