4

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>

2 Answers 2

8

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"))
2
  • 1
    And pressing C-c c on the result will add in the day of the week.
    – SabreWolfy
    May 27, 2015 at 11:03
  • What would be an example of non nil ORG-WITH-TIME?
    – user19777
    Apr 24, 2023 at 23:33
4

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

2
  • 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, 2015 at 11:54
  • @SabreWolfy You'll need to tell Emacs how to retrieve the time string, like with (interactive "r")
    – wasamasa
    May 26, 2015 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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