Here's a function that takes a timestamp range as argument and calculates its duration in minutes:
(defun my/org-timestamp-range-duration-in-minutes (ts)
(if (member (org-element-property :type ts) '(active-range inactive-range))
(/ (time-subtract (org-timestamp-to-time ts 'end) (org-timestamp-to-time ts)) 60)
nil))
We take a timestamp object as argument, check that it's a time range (active or inactive), convert the start and end times to internal format, subtract the start time from the end time which returns seconds and divide by 60 to convert to minutes.
If you want to find out the duration of some timestamp range in your Org mode file, use the following invocation with point
at the beginning of the timestamp (i.e. the opening <
or [
for an active or inactive timestamp resp.):
M-: (my/org-timestamp-range-duration-in-minutes (org-element-timestamp-parser))
E.g. in the following Org mode file, when I run the code block, it moves point
to the beginning of the timestamp (previously determined to be 12 by using C-u C-x =
) and then uses the above invocation to calculate the duration of the timestamp range in minutes:
* APPT foo
<2023-11-22 Wed 20:00-21:00>
* Code
#+begin_src elisp :results drawer
(defun my/org-timestamp-range-duration-in-minutes (ts)
(if (member (org-element-property :type ts) '(active-range inactive-range))
(/ (time-subtract (org-timestamp-to-time ts 'end) (org-timestamp-to-time ts)) 60)
nil))
(save-excursion
(goto-char 12)
(my/org-timestamp-range-duration-in-minutes (org-element-timestamp-parser)))
#+end_src
#+RESULTS:
:results:
60
:end:
If you change the 12 to some other point in the buffer (e.g. 13), the type check fails and the function returns nil
. If you change the timestamp at position 12 to e.g. <2023-11-22 Wed 20:00>
(i.e. not a range), the type check fails there as well. Ranges with different dates work fine: e.g. if you change it to <2023-11-22 Wed 20:00>--<2023-11-23 Thu 21:00>
the block gives a result of 1500 minutes (= 25 hours).
org-get-scheduled-time
returning only the start time - that's becauseSCHEDULED
does not imply duration in Org mode. It is only to notify you that you are supposed to start working on something at the indicated date (and possibly time). See Deadlines and Scheduling in the Org mode manual - particularly the note markedImportant
. As far as Org mode is concerned, it needs to know the start time in order to add the event to the agenda. If you have the grid turned on, a timestamp like the above shows the duration as well...