How to find nearest day(may vary like Sunday, Monday) of a date(variable)

I want to find the nearest day(variable) for a date(variable).
For eg, For 22 Aug, nearest Sunday is 21 Aug and for 25 Aug, nearest Sunday is 28th Aug.
Here both, the date and the nearest day to be found are dynamic.
I have created a Workflow, with variable dates and variable day of the week. But could not get the desired results. (12.0 KB)
Request you to please help.


Can you try the following expression?




1 Like

Thanks for your reply.
But your code, seems to find only the nearest Sunday. I already achieved it using Previous Sunday:
Next Sunday:

My requirement is that the Nearest Day to be found is also dynamic. (It can be Sunday, Monday, Tuesday, Wed, Thursday, Friday, Sat). I can use your code, by adding 7 assign statements like yours. But can there be just one line of code which can solve my requirement.

Hi @SumitTyagi1810

Try this


Refer the xaml

NearestDay.xaml (5.3 KB)


How about the following?

targetDate = New DateTime(2022,8,22)
arr= {-1,-2,-3,3,2,1,0,-1,-2,-3,3,2,1}
wd = CInt(DayOfWeek.Sunday)



Sequence.xaml (6.5 KB)


1 Like

Hello @SumitTyagi1810
Try this mehod using modify date activities and if condition (68.1 KB)

Happy Automation

Gokul Jai

Thanks. It worked.

Made minor changes to make it suitable for loops. Instead of using wd =CInt(DayOfWeek.Sunday), as Day of Week is constant here and was not accepetng dynamic value, I made a dictionary for each weekday and assigned corresponding value of Day of week, and thus used it for skip array evaluations.

1 Like

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