Get a List of Dates Excluding Certain Dates (Public Holidays, Etcetera)

Riddle me this...

I'm thinking KM might be able to help me with this task. I want to enter a date, say Friday 16th December, and then get a list of every Friday for a certain number of weeks excluding particular dates that I would have in a text file. Essentially the academic year. Real case use is providing a list of dates for a class that is going to last x amount of weeks in the academic year.

So a macro that would ask for a start date, and how many weeks, would create a list of all those Mondays for example, and then delete all the public holidays, mid-term breaks etc, but still give me the full 8 weeks or however many I asked for.

The list could be created to clipboard or a textfile in mark down to be pasted into excel or an email.

Does this sound like something KM could do? I currently do it in excel and remove the dates by looking at the calendar. I'm not looking for a macro, just a suggestion as to a plausible way to proceed.

I'm thinking I'm going to convert the start date into an %ICUDateTimeFor% type variable, use %ICUDateTimePlus% to add 7 days, and then append that date to a text file. But before I append it, I would need to reg ex it against another text file containing a list of all unwanted dates (public holidays, etc), and use if/then statements to skip them. Rinse and Repeat until the number of repeats is complete?

Anyone have any words of wisdom before I start to save me from barking up the wrong tree?

Hey Damian,

This is a relatively straightforward task, and Keyboard Maestro can certainly do it.

I'd probably put the excluded dates one per line in a text variable in the macro, unless there were too many – or I found myself editing them often.

I'm thinking I'd use MM/DD without the year for the exclusion list for fixed dates.

Thanksgiving would have to be calculated for the given year, but that's not too difficult.

I'm thinking I would output the excluded dates with an “EXCLUDED” tag, so I would have visual confirmation to eyeball.

Sounds like fun. Enjoy... :sunglasses:

-Chris

May I ask why this doesn't produce a date:

Whereas this does?

You can see that %LD_Startdate% is set to 2022,01,10. I feel like I need to process the tokens or something?

Hey Damian,

I played around with this problem using AppleScript just to wrap my head around the scope of the problem and ended up building a complete macro.

Get a List of Class Dates from User Entered [Date] + [# of Weeks] v1.00.kmmacros (11 KB)

Display-Macro-Image

I have not tested it extensively.

-Chris

Oooh. Exciting. Thanks @ccstone I'll get to testing!

Yep...

I had to add a time too, since the output is GMT.

12,0,0 which translates to 12:00 (pm) works for this and prevents the calculation from losing a day (if you're here in the US).

Date Calculation – Process Tokens v1.00.kmmacros (6.5 KB)

Display-Macro-Image

Hi Chris,

This is awesome. I needed to adapt it to European or (Irish at least) dates. I did so by simply changing all 'MM/dd' to 'dd/MM'. The other thing worth noting is that exceptions seem to need to include the '0' before single digits, that is '01/02' for 1st Feb, not '1/2', which is fine, but in case anyone else wants to use this ever.

We don't have thanksgiving, so I tried to delete this section, but that resulted in an error.

on getThanksgivingDateThisYear(theYear)
	set dateRef to current date
	set year of dateRef to theYear
	set month of dateRef to 11
	set day of dateRef to 20
	set time of dateRef to 0
	repeat while weekday of dateRef ≠ Thursday
		set day of dateRef to (day of dateRef) + 1
	end repeat
	return its formatDate:dateRef usingFormat:"dd/MM"
end getThanksgivingDateThisYear

It is easy to cut and paste the results, but could you tell me how to get the results back into a KM Variable? Then I could do some more formatting from there.

Thanks again. I'd love to have a better grip on AppleScript but fear it may be beyond me given the time I have and my minimal background in programming.

Hey Damian,

I've removed Thanksgiving from this version.

Get a List of Class Dates from User Entered [Date] + [# of Weeks] v2.00.kmmacros (11 KB)

Display-Macro-Image

You were on the right track, but you just removed the Thanksgiving handler and didn't remove the call to the handler. (A handler in AppleScript is a subroutine.)

Yes, that's because I'm using a fixed-length five character substring to test with:

MM/DD

It's much easier to not manage all the possibilities (M/D and MM/D and M/DD and MM/DD) and require the user to use a fixed date format.

Always start by looking at the help for a given action. (Right-Click on the action – bottom of the contextual menu.)

You'll see the options menu at the top of the action allows for saving the output to a variable (amongst other things).

-Chris