Insert the date with the date set to be the next Monday

Hi all,

I’m a complete newbie so go easy on me!

I’m trying to create a template for my weekly plan. The template itself is simple and I’m happy enough creating an action that creates a new draft and inserts some markdown formatted text into the body of the draft.

However, I would like to set some dates in the template automatically. I’m happy enough setting dates and formatting them but I would like the template to set the date to the next monday, which may not be a set number of days away (I usually do my plan on saturday or sunday).

Although not ideal, but as a start, I found a script to bring up a date picker which would allow me to select a date but I can’t understand how to actually then use that in the template. I currently have a 2 step action, the first a script with the date picker and the second an insert text step.

How do I insert the date I selected in the first step into the second step?

Thanks.

Have a look at this action I put together, and see if it helps.

Note you can look into some JavaScript date manipulation too for your original ask. Each day of the week is assigned a number so you can work out how many days you would need to add to the current date and then some date arithmetic and you could output the date of the next Monday without needing to select it.

That’s pretty standard stuff to work with the dates, so asking an AI would probably give you an accurate answer, and if you look in the action you’ll see a setTemplateTag() line. You could use that approach to set your template tag to do this.

Awesome, thank you, will have a look!

I’ve also just realised it was you I was listening to on Automators yesterday! :grin:

1 Like

Another quick note, that Drafts includes the date.js library, so you can do things like:

Let d = Date.today().next().monday()

Maybe helpful.

// Function to get the next Monday and Friday
function getNextWeekdays() {
    const today = new Date();
    const nextMonday = new Date(today);
    const nextFriday = new Date(today);

    // Calculate days to add for next Monday and Friday
    nextMonday.setDate(today.getDate() + (1 + 7 - today.getDay()) % 7 || 7);
    nextFriday.setDate(today.getDate() + (5 + 7 - today.getDay()) % 7 || 7);

    return {
        nextMonday,
        nextFriday
    };
}

// Get the dates
const { nextMonday, nextFriday } = getNextWeekdays();

// Format the dates for UK style
const options = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' };
const formattedMonday = nextMonday.toLocaleDateString('en-GB', options);
const formattedFriday = nextFriday.toLocaleDateString('en-GB', options);

// Create the text to insert
const draftContent = `Weekly plan -- ${formattedMonday} to ${formattedFriday}`;

// Insert the text into the draft
draft.content += draftContent;

// Optionally, you can save the draft if needed
draft.update();

Ok, so I cheated and got ChatGPT to write the above code for me which works perfectly :smile:

I’ve also added an insert text section to add some simple formatted text after the above.