Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get previous weekday's date

Posted on 2009-05-10
3
Medium Priority
?
1,646 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 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

879 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