# Solved: Get the last saturday date for comparaison whenever the script is lunched

Hello Guys

I would like to share with you a node that compare the date when the script is lunched (today date) and do a true state if it’s after the prior saturday
The use can be as exemple : get all file since last saturday

In my exemple i compared the date of creation from file of the previous node

## Share the output returned by the last node

note: I’m not a dev. and i used ChatGPT to solved it.
let’s take a closer look of the code

``````Me : question :
can you write me the calcul for this code "{{new Date(new Date().getTime() - (new Date().getDay() === 7 ? 0 : 5 - new Date().getDay()) * 24 * 60 * 60 * 1000).toISOString().slice(0, 10)}}" when the date of application is 2023-02-07

ChatGPT:
Sure! Let's take a step-by-step approach to calculate the expression for 2023-02-07:

new Date().getTime() returns the current date and time in milliseconds since January 1, 1970. For the date 2023-02-07, the result would be something like 1612703378000.

new Date().getDay() returns the day of the week, where Sunday is 0 and Saturday is 6. So, for the date 2023-02-07, the result would be 2 (Tuesday).

5 - new Date().getDay() returns the number of days until the last Saturday, which in this case is 5 - 2 = 3.

(new Date().getDay() === 7 ? 0 : 5 - new Date().getDay()) * 24 * 60 * 60 * 1000 calculates the number of milliseconds until the last Saturday. In this case, it would be 3 * 24 * 60 * 60 * 1000 = 259200000.

new Date().getTime() - (new Date().getDay() === 7 ? 0 : 5 - new Date().getDay()) * 24 * 60 * 60 * 1000 calculates the date and time in milliseconds for the last Saturday before 2023-02-07. In this case, it would be 1612703378000 - 259200000 = 1612344178000.

new Date(new Date().getTime() - (new Date().getDay() === 7 ? 0 : 5 - new Date().getDay()) * 24 * 60 * 60 * 1000) creates a new Date object from the calculated date and time in milliseconds.

.toISOString().slice(0, 10) returns the ISO string representation of the date object and then slices only the first 10 characters, which is the date in the format YYYY-MM-DD. So, for the date 2023-02-07, the final result would be 2023-01-28.
``````

## Information on your n8n setup

• n8n version:
• Database you’re using (default: SQLite):
• Running n8n with the execution process [own(default), main]:
• Running n8n via [Docker, npm, n8n.cloud, desktop app]:
1 Like

Sweet, thanks for sharing

You might also want to take a look at Luxon which makes working with dates a little easier in most cases.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.