Solved

Get previous weekday's date

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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