Solved

C# subtract 1 day from current day only when weekdays

Posted on 2011-09-20
8
460 Views
Last Modified: 2012-05-12
I have this line which subtracts 1 day from the current date (to populate a textbox):

DateTime.Now.AddDays(-1).ToShortDateString();

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.





0
Comment
Question by:RustyZ32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 7

Expert Comment

by:NauticalNonsense
ID: 36569288
DateTime dateTime = DateTime.Now;
                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?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 36569299
Lots of ways to do it...

            DateTime dt = DateTime.Today.AddDays(-1);
            while (dt.DayOfWeek == DayOfWeek.Sunday || dt.DayOfWeek == DayOfWeek.Saturday)
                dt = dt.AddDays(-1);
            string ShortDate = dt.ToShortDateString();
0
 
LVL 7

Expert Comment

by:NauticalNonsense
ID: 36569313
Pretty sneaky, sis.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:RustyZ32
ID: 36569324
Ok, how can I set a textbox value using these?

my old line is as follows:

TBenddate.Text = DateTime.Now.AddDays(-1).ToShortDateString();

I've tried a few different ways and can't seem to get the syntax correct.
0
 
LVL 7

Assisted Solution

by:NauticalNonsense
NauticalNonsense earned 100 total points
ID: 36569335
using Idle's code:

TBenddate.Text = ShortDate;
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36569347
*Tag Team* Thanks Nautical.  =)
0
 
LVL 6

Author Closing Comment

by:RustyZ32
ID: 36569379
that worked! I changed the values to monday and sunday to test, worked like a charm.


thanks to you both.
0
 
LVL 7

Expert Comment

by:NauticalNonsense
ID: 36569542
Your loop was adorable... love it!
Cheers!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question