How to insert a relative date?

I often insert the current date. This way

Can I change this (and how) so that relative to the current date I can choose to insert yesterday's or tomorrow's date? Thanks!

You'd need to use the "Run Real Java Script" action.

Example for tomorrows date: (PLUS 24 hours * 60 minutes * 60 seconds * 1000 milliseconds)

async function pasteDate() {

let theDate = new Date(new Date().getTime() +  24 * 60 * 60 * 1000);
let day = theDate.getDate()
let month = theDate.getMonth() + 1
let year = theDate.getFullYear()

let dateFormatted = `${day}.${month}.${year}`

paste_text({text: dateFormatted});

return "done";
}

Example for yesterday's date (MINUS 24 hours * 60 minutes * 60 seconds * 1000 milliseconds)

async function pasteDate() {

let theDate = new Date(new Date().getTime()  -  24 * 60 * 60 * 1000);
let day = theDate.getDate()
let month = theDate.getMonth() + 1
let year = theDate.getFullYear()

let dateFormatted = `${day}.${month}.${year}`

paste_text({text: dateFormatted});

return "done";
}

Ah apparently it's better to do it this way:

async function pasteDate() {

let theDate = new Date();
theDate.setDate(theDate.getDate() + 1); // +1 means tomorrow, -1 would be yesterday
let day = theDate.getDate()
let month = theDate.getMonth() + 1
let year = theDate.getFullYear()

let dateFormatted = `${day}.${month}.${year}`

paste_text({text: dateFormatted});

return "done";
}

Ah, perfect, I'll try that out, thanks.

Then, by analogy, 2 days in the future would be doubling this, and so on?

24 * 60 * 60 * 1000

Try this for two days in the future, my original approach using the milliseconds of a day has some disadvantages. This is better:

async function pasteDate() {

let theDate = new Date();
theDate.setDate(theDate.getDate() + 2); // + 2 means 2 days in the future
let day = theDate.getDate()
let month = theDate.getMonth() + 1
let year = theDate.getFullYear()

let dateFormatted = `${day}.${month}.${year}`

paste_text({text: dateFormatted});

return "done";
}

By default the "paste_text" function does "Insert By Typing", you can change it to "Insert By Pasting" like this:

async function pasteDate() {

let theDate = new Date();
theDate.setDate(theDate.getDate() + 2);
let day = theDate.getDate()
let month = theDate.getMonth() + 1
let year = theDate.getFullYear()

let dateFormatted = `${day}.${month}.${year}`

paste_text({text: dateFormatted, insert_by_pasting: true});

return "done";
}
1 Like