Solved

Given a date get the previous date which is not a sat or sunday

Posted on 2013-10-24
2
214 Views
Last Modified: 2013-11-05
How can I get the previous date for DateTime.Today which is not a sat or sunday
0
Comment
Question by:countrymeister
[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
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 39597992
Lots of ways...here's one:
            DateTime dt = DateTime.Today;
            dt = dt.AddDays(-1);
            while (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
            {
                dt = dt.AddDays(-1);
            }

Open in new window

0
 
LVL 2

Expert Comment

by:Pavel_Kudishin
ID: 39619729
DateTime today = DateTime.Today;
switch (today.DayOfWeek)
{
    case DayOfWeek.Monday:
        return today.AddDays(-3);
    case DayOfWeek.Sunday:
        return today.AddDays(-2);
    default:
        return today.AddDays(-1);
}

Open in new window

0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
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…

628 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