RustyZ32
asked on
C# subtract 1 day from current day only when weekdays
I have this line which subtracts 1 day from the current date (to populate a textbox):
DateTime.Now.AddDays(-1).T oShortDate String();
How can I do the same but have it ignore weekend days? for example, on monday it would subtract 1 day but skip over sunday and saturday to return the value for friday.
In other words, how can I subtract 1 weekday from the current day.
DateTime.Now.AddDays(-1).T
How can I do the same but have it ignore weekend days? for example, on monday it would subtract 1 day but skip over sunday and saturday to return the value for friday.
In other words, how can I subtract 1 weekday from the current day.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Pretty sneaky, sis.
ASKER
Ok, how can I set a textbox value using these?
my old line is as follows:
TBenddate.Text = DateTime.Now.AddDays(-1).T oShortDate String();
I've tried a few different ways and can't seem to get the syntax correct.
my old line is as follows:
TBenddate.Text = DateTime.Now.AddDays(-1).T
I've tried a few different ways and can't seem to get the syntax correct.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
*Tag Team* Thanks Nautical. =)
ASKER
that worked! I changed the values to monday and sunday to test, worked like a charm.
thanks to you both.
thanks to you both.
Your loop was adorable... love it!
Cheers!
Cheers!
DateTime prePopulate = new DateTime();
if (dateTime.DayOfWeek == System.DayOfWeek.Monday)
prePopulate = dateTime.AddDays(-3);
else
prePopulate = dateTime.AddDays(-1);
What are you doing on Saturday and Sunday? Or are you assuming nobody uses this on Sunday?