Solved

C# subtract 1 day from current day only when weekdays

Posted on 2011-09-20
8
452 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
Comment Utility
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
Comment Utility
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
Comment Utility
Pretty sneaky, sis.
0
 
LVL 6

Author Comment

by:RustyZ32
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Assisted Solution

by:NauticalNonsense
NauticalNonsense earned 100 total points
Comment Utility
using Idle's code:

TBenddate.Text = ShortDate;
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
*Tag Team* Thanks Nautical.  =)
0
 
LVL 6

Author Closing Comment

by:RustyZ32
Comment Utility
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
Comment Utility
Your loop was adorable... love it!
Cheers!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now