Solved

Get previous weekday's date

Posted on 2009-05-10
3
1,537 Views
Last Modified: 2013-12-17
Hi all,

C# has some good date functions, but i can't figure out how to code this one.
(My C# and dates is abit rusty)

I want to find out the date of the previous week day.
So for example Today it is Monday 11-May-2009, Or Sunday 10th if your in the US. (So the DateTime.now is good for that)
So for this example the previous weekday would be Friday 8-may-2009.
Now if the current day is Tuesday -> Friday the previous week day is simple, It's
DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy")

Am I specifically going to have to check if the weekday is a Saturday/Sunday/Monday and subtract accordingly or is there a better way?
0
Comment
Question by:qz8dsw
3 Comments
 
LVL 18

Accepted Solution

by:
philipjonathan earned 250 total points
ID: 24350874
I think checking the current weekday for sat/sun/mon is the easiest. You can use DateTime.DayOfWeek property.

public DateTime PreviousWeekDay(DateTime currentDate)
{
  DayOfWeek dow = currentDate.DayOfWeek;
  switch (dow)
  {
    case DayOfWeek.Sunday:
      return currentDate.AddDays(-2);
    case DayOfWeek.Monday:
      return currentDate.AddDays(-3);
    default:
      return currentDate.AddDays(-1);
  }
}
0
 
LVL 7

Assisted Solution

by:zwei
zwei earned 250 total points
ID: 24350917
How about this loop:
DateTime dt = DateTime.Now;

do 

{

    dt = dt.AddDays(-1);

}

while ((int) dt.DayOfWeek < 1 || (int)dt.DayOfWeek > 5);

Open in new window

0
 
LVL 15

Author Closing Comment

by:qz8dsw
ID: 31580000
Thanks zwei and philip
Was hoping C# had a nicer solution, but both your comments confirmed it doesn't.
You both offered slightly differing ways to achieve the same thing. Both very good responses.

Cheers,
Terry
Terry
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

22 Experts available now in Live!

Get 1:1 Help Now