Solved

C# subtract 1 day from current day only when weekdays

Posted on 2011-09-20
8
455 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 31
PrintingFoundIt(Almost!(Damn!)) 1 27
start a process from a service 3 23
RLDC Reporting in Visual studio 11 16
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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