Solved

Get previous weekday's date

Posted on 2009-05-10
3
1,572 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
[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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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