Macro for "Next Sunday"

This is driving me nuts! I found a very useful discussion here on how to calculate the date for a specific day in the future. From that, I modified it so I could get the output in the format I wanted, with dashes. I ended up with:

%ICUDateTimePlus%(7+5-DOW(NOW)) MOD 1%Days%MM-d-yy%

This gives me the date for next Sunday, but the wrong day of the week. In this case: Monday 12-25-16.

I understand the TimePlus part of it for adding time, the DOW is Day Of the Week, but I don’t understand MOD.

I also want a macro for “last Sunday” but I can’t get that to work either. I tried TimeMinus, I tried changing the numbers in the parenthesis and the MOD part, and just can’t crack it. Spent time reading the manual, still having trouble. Can someone save me from spending another couple hours on this? I just want a formula for last Sunday’s date in dash form, and one for next Sunday’s date in the same format. So close, but no cigar! And it’s killing me that I can’t figure it out myself. :rage:

See if this topic will answer your question:

That’s the one I referenced above, and it was your explanation in that post that I studied for so long last night and put me on the right path, so thank you. I still don’t get how it works though, mechanically – why the 7+5-… part? And what does the MOD part mean? When I tweak it, I get random stuff because I don’t understand those bits.

See if this macro (now Ver 2.4) works for you:

2016-12-27 2:28 PM CT

  • Corrected formula for PRIOR Day to be the first day prior to today.

##Macro Library   @DATE Get Date for Prior/Next Day of Week [Example]


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/0/033924497a09702d1933851cc510c493575481bb.kmmacros">@DATE Get Date for Prior-Next Day of Week [Example].kmmacros</a> (12 KB)

---

### Example Results

If TODAY is Tue, Dec 27, 2016
then the results will be:

```applescript

DayNum Date


-1	Sun, Dec 25, 2016
-3	Tue, Dec 20, 2016
-7	Sat, Dec 24, 2016
1	Sun, Jan 1, 2017
3	Tue, Jan 3, 2017
7	Sat, Dec 31, 2016
```

---

For those that prefer the minimalist approach, see [Ver 2.4.1 below](https://forum.keyboardmaestro.com/t/macro-for-next-sunday/5851/21?u=jmichaeltx).


###ReleaseNotes

Author.@JMichaelTX

**PURPOSE:**

* **Get the Date for the NEXT or PRIOR Day of Week (Sun, Mon, Tue, ...)**

HOW TO USE:

1. Set the Day of Week Number in the magenta-colored Action below
  (1 = Sun;  Negative for PRIOR, Positive for NEXT)
.
2. Trigger this Macro

NOTE: If the Next/Prior Day requested is the same day of week as today, then it wlll return one week from today.

VERSION HISTORY

2.4 2016-12-27 1:55 PM CT

  • Fixed bug in PRIOR Offset. New formula is:
    • (((7 - DOW( ) + ABS(DATE__DayNum)) MOD 7) ) - 7

2 Likes

Just updated the macro.

Another update to correct unneeded time zone correction.
Apparently the KM Wiki is incorrect about this, or I am totally misunderstanding it. But it seems to work now for me.

My local time is still Sat, Dec 24, whereas GMT is Sun, Dec 25.
My macro seems to work OK with this.

Please me me know if you see any issues.

Something is still wrong. Setting daynum to 3 ought to produce next Tuesday, but results in next Thursday. Setting daynum to -3 ought to produce last Tuesday, but results in next Tuesday.

My apologies for all the errors. I could have sworn that it was giving correct results in my testing, but obviously I missed a bug.

Let's hope this time is a charm, and everything works.
I just posted an update:

BTW, I'm running Keyboard Maestro 7.3.1 (7.3.1) on macOS 10.11.4, in the US Central Time zone.

If the macro is not working for you now, please post your versions and TZ.

Thanks.

Bingo! I tested all the cases and they work properly (at least in Central TZ, running latest KM and macOS versions).
Thanks very much for all your effort on this (and everything else you do here)

1 Like

That is very good hear. If any issues concerning this macro popup later, feel free to post here, and I'll take a look at them.

If my macro/post solves your problem/question, please check the "Solved" checkbox at the bottom of my above post.

Otherwise, please post your remaining questions/issues.

Any idea why I don’t see the “solved” checkbox - I looked on the discourse help pages and see where it is supposed to show up, but all I see are the heart to like the post, the link icon to share the post, the flag icon to privately flag it and the bookmark icon. No solved checkbox anywhere to be seen…
Does it go away if someone else has already checked it?

If you don't see it, you have to click on the "More" button:

As the owner of this topic, you have the right to select, or change, the "Solved" post, even if it is checked by someone else (which could only be a moderator).

Ahh, I am not the owner of the topic. (I did click the more button)

That explains things. I totally overlooked ownership.

So, we will need to wait for @Steve_Solari, the topic owner, to confirm this macro solves his question/problem.

Nice, thank you very much! Did not expect this much attention to the topic at all. Really appreciate all the work you put into it. Surprised it takes so much code to get, but I’m not a developer so it’s probably just my ignorance. Works as expected!

Is there an easy way to get “the most recent Sunday?” For example, when I run this macro I get the Sunday of last week. What if I wanted the date of the most recent Sunday, regardless of the week? For example, now, on Tuesday, the desired result would be “Sun, Dec 25, 2016” instead of “Sun, Dec 18, 2016.” Or say it’s Tuesday, and I want the date of this coming Friday, not the Friday of next week? Would there have to be a conditional statement that sets the current week instead of the one before or after this one?

I just revised the above post with a new version, #2.4.

This should fix the bug/issue reported by:

The macro should now return the date of the nearest PRIOR/NEXT day, regardless of which week it is in. So now,

For TODAY of Tue, Dec 27, 2016

  • "-1" will return
  • Sun, Dec 25, 2016

Also, please note that If the Next/Prior Day requested is the same day of week as today, then it will return one week from today.

@Steve_Solari and @rolian, please test this new version, and confirm proper calculation, or report any bugs/issues you find.

Thanks to both of you for your patience in this, as I have worked out the bugs in this algorithm. It has turned out to be tougher than I originally thought.

The macro is probably more verbose that it absolutely needs to be.
Once we have verified the accuracy, I'll try to provide a version that uses only 2 or 3 (maybe less) Actions.

Don't thank me for patience, thank YOU for your efforts!

I changed the output to this to fit my needs:

and I get this:

Monday Sun, Dec 25, 2016

Did I trim back too much, or the wrong code? Before, it was:

Wait, I think I have an autocorrect issue getting in the way… Testing…

Ha! Found it – TextExpander had the same trigger expanding something different and was corrupting the results. Works great for me now, exactly what I was looking for. Thanks again @JMichaelTX !

1 Like

That's very good to hear!
You're quite welcome!