I have the following variable:
DateTime zonaSemana = DateTime.Today.AddDays(((int)DayOfWeek.Wednesday - (int)DateTime.Today.DayOfWeek) == 0 ? 7 : ((int)DayOfWeek.Wednesday - (int)DateTime.Today.DayOfWeek + 7) % 7).AddSeconds(1);
Is there any way to make it shorter? It comes to me the date of the following Wednesday.
That is, I have the following:
This adds 10 min:
zonaMins = DateTime.Now.AddMinutes(10);
This 24 hours:
zonaDias = new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 0, 0, 1);
But the 1 week one doesn't work well for me and I don't know why. It has to give me Wednesday and I don't know if it's because it's too long... or something.
Thank you
Well, the problem is that if you run the code on the same day of the week that you're looking for, it doesn't give you the following Wednesday, but the Wednesday you're already on. That's easily fixed by finding the next Tuesday, instead of Wednesday, and adding a day to it. I mean:
What you can do is:
This will always return to you next Wednesday (if today is Wednesday and you want it to return the same day, change the
do while
for only thewhile
).