# Date question

How do I get the date for next immediate Wednesday? For example based on today’s date, I need to find next immediate Wednesday. Thanks for your help.

Hi @cury

Try this expression

``````If(DateTime.Now.AddDays(5-CInt(DateTime.Now.DayOfWeek))<=DateTime.Now,DateTime.Now.AddDays(5-CInt(DateTime.Now.DayOfWeek)).AddDays(7),DateTime.Now.AddDays(5-CInt(DateTime.Now.DayOfWeek)))
``````

Regards
Gokul

Try this expression also

``````Today.AddDays((Today.DayOfWeek - DayOfWeek.Wednesday)*-1)
``````

Regards
Gokul

Thank you so much @Gokul001. Let me try this. Does it work for running on any weekday. Does it always get me date for next immediate Wednesday? Thank you,

Hi @cury

Try the below expression

``````DateTime.Now.AddDays(+7).AddDays(-1*(7 + (DateTime.Now.DayOfWeek - DayOfWeek.Wednesday)) mod 7).Date.ToString()
``````

Hi @cury ,

Since you asked for the next Wednesday, then this will do the trick->

``````If(Today.AddDays(Today.DayOfWeek - DayOfWeek.Wednesday).Equals(Today),Today.AddDays(7),Today.AddDays(Today.DayOfWeek - DayOfWeek.Wednesday))
``````

Could you test it out and see if it works as expected?

Kind Regards,
Ashwin A.K

Perfect! Thanks a lot for simplified solution.

Great @ashwin.ashok

Happy automation

Regards
Gokul

Hi @cury ,

Just to clarify, if today is Wednesday, then did you want to retrieve today’s date, or the next Wednesday?

Kind Regards,
Ashwin A.K

If today is wednesday, I need to get today’s date. If running tomorrow, Friday and so on, I need to get next Wednesday. Thank you

Alright got it!

Kind Regards,
Ashwin A.K

@Gokul001
I am trying to generalize this. In the above, I replaced “Wednesday” with “Tuesday”. I was hoping to get 03/22/22. Instead I am getting 03/15. Just wondering why. thanks,

HI @cury

Try this expression

``````DateTime.Now.AddDays(+7).AddDays(-1*(7 + (DateTime.Now.DayOfWeek - DayOfWeek.Tuesday)) mod 7).Date.ToString()
``````

Regards
Gokul

Sorry but, for Wednesday, it is not giving today’s date. It is giving 03/23/22.
Thank you,

Hi,

FYI, another solution:

``````td = DateTime.Today
``````

Regards,

@ashwin.ashok
When today is a Wednesday, I expect to get today’s date 03/16. It is getting me only next Wednesday 03/23. Thank you,

Hi @cury ,

@Gokul001 code should have worked actually.
If it hasn’t do give @Yoichi -san’s a try, and if not that then give this a try →

``````Today.AddDays(Today.DayOfWeek - DayOfWeek.Wednesday)
``````

Kind Regards,
Ashwin A.K

@Yoichi
Thanks, Do I have to adjust the formula if it is another week day. For example, if I replace Wednesday with Thursday or Friday I am getting 03/16/22. Thank you,

Hi,

I just modified the above expression to the following.

``````td.AddDays(if(td.DayOfWeek>DayOfWeek.Wednesday,7,0)-CInt(td.DayOfWeek)+Cint(DayOfWeek.Wednesday))
``````

We can change DayOfWeek.Wednesday to any other day of week such as DayOfWeek.Friday.

Regards,

@ashwin.ashok Getting 03/15/22 for Thursday. Thanks