Solved

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

Posted on 2013-10-24
2
201 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
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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