6

At the last weekday of the month I want to be reminded to prepare invoice/expenses so I get them filed by the first. It's no use just getting reminded on the last day of the month, if this is a Saturday or Sunday. Nor do I want to be reminded several days before the end of the month, so using diary-float is not applicable.

Stig Brautaset
  • 732
  • 5
  • 13
  • See also https://emacs.stackexchange.com/questions/31683/schedule-org-task-for-last-day-of-every-month – dat Apr 08 '21 at 04:24

1 Answers1

5

Answering myself, I came up with this. I created a function to detect whether a given date is the last weekday of the month.

There's two main cases: if it's the last day of the month and it's a weekday, then obviously it's the last weekday of the month. Otherwise, the last weekday of the month must be a Friday, and it will be either the last-but-one or last-but-two day of the month. This logic can be easily expressed in emacs-lisp (put it in your ~/.emacs):

(defun last-weekday-of-month-p (date)
  (let* ((day-of-week (calendar-day-of-week date))
         (month (calendar-extract-month date))
         (year (calendar-extract-year date))
         (last-month-day (calendar-last-day-of-month month year))
         (month-day (cadr date)))

    (or
     ;; it's the last day of the month & it is a weekday
     (and (eq month-day last-month-day)
          (memq day-of-week '(1 2 3 4 5)))

     ;; it's a friday, and it's the last-but-one or last-but-two day
     ;; of the month
     (and (eq day-of-week 5)
          (or (eq month-day (1- last-month-day))
              (eq month-day (1- (1- last-month-day))))))))

This can then be used from my Diary file like so:

%%(last-weekday-of-month-p date) File Invoice
Stig Brautaset
  • 732
  • 5
  • 13