Is it possible to convert plain text dates to Org dates automatically? I have entries in the format:

16 February 2015

which I would like to convert to Org dates: <2015-02-16 Mon>

up vote 6 down vote accepted

Internally, org-mode uses the function org-read-date to do this:

(org-read-date &optional ORG-WITH-TIME TO-TIME FROM-STRING PROMPT
DEFAULT-TIME DEFAULT-INPUT INACTIVE)

For example,

(org-read-date nil nil "16 February 2015")

returns "2015-02-16". This doesn't put in the "<>" or "[]" for active/inactive time-stamps, but you can easily add them with a format:

(format "<%s>"   (org-read-date nil nil "16 February 2015"))
  • 1
    And pressing C-c c on the result will add in the day of the week. – SabreWolfy May 27 '15 at 11:03

I came up with something like this:

(defun my-format-time (time-string)
  (let* ((time (parse-time-string time-string))
         (day (nth 3 time))
         (month (nth 4 time))
         (year (nth 5 time)))
    (format-time-string "<%Y-%m-%d>" 
      ;; I would love to do an 'apply here, but the given list to be encoded contains nil which does n
      (encode-time 0 0 0 day month year))))

With that you can create an interactive function that scans for your specific date format and replaces the found date strings with the desired format.

For further reading have a look at:

http://ergoemacs.org/emacs/elisp_parse_time.html http://www.gnu.org/software/emacs/manual/html_node/elisp/Time-Parsing.html

  • Can you explain how to use this? I added (interactive) on the second line, selected date text and ran the function, but got an error about parameters. – SabreWolfy May 26 '15 at 11:54
  • @SabreWolfy You'll need to tell Emacs how to retrieve the time string, like with (interactive "r") – wasamasa May 26 '15 at 11:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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