6

I have an org-mode document in which I wish to keep track of the number of days that have passed since a certain date. Is there a built in function for this, or will I be forced to write a custom elisp function to accomplish this?

5

I write "<2016-06-15 Wed>--<2016-07-18 Mon>" ...and then (with POINT somewhere over the dates) I press C-c C-y (org-evaluate-time-range) C-ucy will write the time-range after the dates like, <2016-06-15 Wed>--<2016-07-18 Mon> 33d

  • This was exactly what I was looking for. Thanks! :-) – davorb Jun 23 '16 at 19:49
5

The original poster may wish to have a look at the built-in function called calendar-count-days-region described in the manual: https://www.gnu.org/software/emacs/manual/html_node/emacs/Counting-Days.html

The following is a custom function that uses the org-mode and calendar-mode libraries. Upon my examination of calendar-count-days-region, I saw that the author included (in the count) the day at the end of the region (i.e., by programmatically adding one day). In my line of work, counting the last day as part of the total is not permitted -- so I would use something like the following example instead (which does not add an additional day to the total count).

(require 'calendar)
(require 'org)

(defun count-calendar-days ()
"Count the number of calendar days -- includes holidays, weekends, etc."
(interactive)
  (let* (
      (d1 (org-read-date nil nil nil "Insert First Date:  "))
      (d1-parsed (org-parse-time-string d1))
      (d1-day (nth 3 d1-parsed))
      (d1-month (nth 4 d1-parsed))
      (d1-year (nth 5 d1-parsed))
      (d1-list (list d1-month d1-day d1-year))
      (d2 (org-read-date nil nil nil "Insert Second Date:  "))
      (d2-parsed (org-parse-time-string d2))
      (d2-day (nth 3 d2-parsed))
      (d2-month (nth 4 d2-parsed))
      (d2-year (nth 5 d2-parsed))
      (d2-list (list d2-month d2-day d2-year))
      (date1 (calendar-absolute-from-gregorian d1-list))
      (date2 (calendar-absolute-from-gregorian d2-list))
      (total-days
        (let* ((days (- (calendar-absolute-from-gregorian d1-list)
                        (calendar-absolute-from-gregorian d2-list)))
               (days (if (> days 0) days (- days))))
          days)) )
    (message "%s (+/-) %s = %s" d1 d2 total-days)))

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.