Solved

Get previous weekday's date

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
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…

734 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