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

Posted on 2013-10-24
Medium Priority
Last Modified: 2013-11-05
How can I get the previous date for DateTime.Today which is not a sat or sunday
Question by:countrymeister
LVL 86

Accepted Solution

Mike Tomlinson earned 1200 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


Expert Comment

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

Open in new window


Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.
SingleRun is a tool that ensures that only one instance of an application is started, running it again brings the application to focus.
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Suggested Courses

621 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