One of my daily tasks involves making a log entry in which I have to use yesterday’s date. That’s pretty easy with Org dates but the dates in the log don’t have the usual Org markup of square or angle brackets. They’re just plain dates like this: 2021-01-16
. I have a function to enter dates like these bound to Ctrl+c d. For a long time, I just used this shortcut to enter today’s date and fixed it manually. That was easy enough except at a month change but was still pretty silly.
A couple of years ago, I got fed up and decided to write a function that would directly enter yesterday’s date. Getting yesterday’s date is way harder than it should be but after a bit of research, I found the secret spell and wrote the function. Recently, I’ve had a need to enter the date from two days ago. The first couple of times I just entered yesterday’s date with my function and fixed it manually but rather than go through another long period of making manual adjustments, I thought I’d just fix my function to take a numerical argument so that it could enter a day from \(n\) days ago.
Unlike the original function, the change was trivial. If you have a similar need, here’s the function:
1: (defun jcs-yesterday (days) 2: "Insert yesterday's date." 3: (interactive "p") 4: (let ((ts (decode-time))) 5: (setf (nth 3 ts) (- (nth 3 ts) (if days days 1))) 6: (insert (format-time-string "%Y-%m-%d" (apply #'encode-time ts)))))
As an (unintended) side effect, I can even enter a negative number and get a date \(n\) days in the future. If your use case involves entering dates in the future, you can change the meaning of \(n\) so that positive arguments move forward by changing the \(-\) sign in line 5 to \(+\).