Show me your scripts!

I propose the following enhancement to the script, which is more flexible because it allows the user to choose the desired day of the week from a popup. So, instead of creating 7 different snippets for each day of the week, we can have just one:

// 1. Map weekday names to numbers
let dayMap = {
  "Sunday": 0,
  "Monday": 1,
  "Tuesday": 2,
  "Wednesday": 3,
  "Thursday": 4,
  "Friday": 5,
  "Saturday": 6
};

// 2. Prompt the user for the desired weekday
let choice = "%fillpopup:name=Select the weekday you want to find the next date for:default=Monday:Tuesday:Wednesday:Thursday:Friday:Saturday:Sunday%";
let targetWeekday = dayMap[choice];

// 3. Calculate the next occurrence of the selected weekday
let now = new Date();
let daysAhead = (targetWeekday - now.getDay() + 7) % 7 || 7;
let nextDate = new Date(now.getTime() + daysAhead * 24 * 60 * 60 * 1000);

// 4. Format the date
let weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

let weekday = weekdays[nextDate.getDay()];
let month = months[nextDate.getMonth()];
let day = nextDate.getDate();
let year = nextDate.getFullYear();

// 5. Output example: "Tuesday (May 14, 2025)"
weekday + " (" + month + " " + day + ", " + year + ")";
%filltop%

This script uses the key: value format for redefining popups. I also mentioned this format here. It’s a super useful approach because it lets you present user-friendly labels in the dropdown while returning clean, machine-friendly values behind the scenes.

2 Likes