Set date to the closest Saturday

I need to set a variable to be the date of the closest Saturday, the process only runs Thur - Mon, so on the Thur and Fri it needs to pick up the date of the next Saturday, on the Saturday it needs to use that day’s date, on the Sunday and Monday it needs to use the date of the previous Saturday.

So for this week, it would run on the 25th, 26th, 27th, 28th and 29th and for each of those it should use the 2024-01-27

Hi @caroline.bradshaw

Try the below one,

closestSaturday = (If(DateTime.Now.DayOfWeek = DayOfWeek.Saturday, DateTime.Now, 
                            If(DateTime.Now.DayOfWeek = DayOfWeek.Sunday, DateTime.Now.AddDays(-1).Date, 
                               If(DateTime.Now.DayOfWeek = DayOfWeek.Monday, DateTime.Now.AddDays(-2).Date, 
                                  DateTime.Now.AddDays((DayOfWeek.Saturday - DateTime.Now.DayOfWeek + 7) Mod 7).Date)))).ToString("yyyy-MM-dd")

closestSaturday is of DataTypse System.String.

Hope it helps!!

1 Like

You could also loop forward with adddays(1) instead of adddays(-1)

1 Like

If you prefer to use activities…

image
image
image
image

The Else expression: DateTime.Now.AddDays((DayOfWeek.Saturday - DateTime.Now.DayOfWeek + 7) Mod 7).Date.ToString("MM/dd/yyyy")

1 Like

Is it working for you @caroline.bradshaw

If yes, Make my post mark as solution to close the loop, else if you have any doubts further post here I am happy to help :smiley:

Happy Automation!!

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