Solved

Find Next Saturday Date from a known Date Value using C#

Posted on 2008-10-06
5
2,404 Views
Last Modified: 2013-12-17
Hello Experts,

Does anyone know how to find the next Saturday date from a known date?

In other words, Say you have a date variable with a value of 10/1/2008
Is there a way to autimatically change the variable to the next Saturday date (10/4/2008 in this case)??

Thanks!
0
Comment
Question by:Saxitalis
  • 3
5 Comments
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
you could make a method that takes a date and adds a day to it while the dates.dayofweek property isn't saturday.

0
 
LVL 22

Accepted Solution

by:
p_davis earned 500 total points
Comment Utility
DateTime GetNextSaturday(DateTime time)
{
       DateTime startTime = time.AddDays(1);

      while(startTime.DayOfWeek != DayOfWeek.Saturday)
      {

                startTime = startTime.AddDays(1);
      }

 return startTime;

}
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
id just did this without an editor so it will need to be tested -- but the general idea should get you what you need.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Instead of looping, just use the DayOfWeek property directly to compute how many days to add:

    DateTime GetNextSaturday(DateTime time)
    {
        return time.AddDays(DayOfWeek.Saturday - time.DayOfWeek);
    }

Here is a generic helper function to get the next day of week that you specify:

    private void Button1_Click(object sender, System.EventArgs e)
    {
        System.DateTime Saturday = GetNextDay(System.DateTime.Today, DayOfWeek.Saturday);
        MessageBox.Show(Saturday.ToString("dddd") + ", " + Saturday.ToString("MMMM dd, yyyy"));
    }

    private System.DateTime GetNextDay(System.DateTime dt, DayOfWeek day)
    {
        return dt.AddDays(day - dt.DayOfWeek);
    }
0
 

Author Closing Comment

by:Saxitalis
Comment Utility
Thank you very much sir!

I modified your code a bit in case it was a Saturday sent in, I don't want the dat to increment
{
            //initialize startTime
            DateTime startTime = time.AddDays(0); // (0) instead of (1)
            while (startTime.DayOfWeek != DayOfWeek.Saturday)
            {

                startTime = startTime.AddDays(1);
            }
            return startTime;
        }
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

17 Experts available now in Live!

Get 1:1 Help Now