Solved

C# subtract 1 day from current day only when weekdays

Posted on 2011-09-20
8
456 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
  • 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 85

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 85

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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