tbaseflug
asked on
Get previous Friday
I need a way to always get the previous friday in a string?
If you have a DateTime instance, then you can use a loop to find the previous Friday:
DateTime d = DateTime.Now;
while (d.DayOfWeek != DayOfWeek.Friday)
{
d = d.AddDays(-1);
}
ps... did not test
kaufmed your's is way more streamlined but it won't work if today's date is friday and you still want the last friday.
(nice work btw)
(nice work btw)
also -- there is no subtractdays =) sorry
so you will have to use the .AddDays(-x);
ASKER
SubtractDays? is that c# compat?
but it won't work if today's date is friday and you still want the last friday.
Aww... shucks ={D
How about:
while (d.DayOfWeek != DayOfWeek.Friday)
{
d = d.AddDays(1);
}
d = d.AddDays(-7);
ASKER
very strange - both solutions return 10/28/2011 - I checked the date on the server and it is marked for today...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@AndyAinscow
You shouldn't need the alteration to the logic. If today is Friday, then you wouldn't want to enter the loop, and you would simply subtract seven days. The addition means there will be one needless iteration of the loop.
You shouldn't need the alteration to the logic. If today is Friday, then you wouldn't want to enter the loop, and you would simply subtract seven days. The addition means there will be one needless iteration of the loop.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am an idiot - wrong calendar - whoops
DateTime tempDate = new DateTime();
while(DateTime.Now.Subtrac
{
daysSub++;
tempDate = DateTime.Now.SubtractDays(
}
DateTime yourLastFriday = tempDate;