Solved

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

Posted on 2008-10-06
5
2,412 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
ID: 22650737
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
ID: 22650764
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
ID: 22650769
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
ID: 22651573
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
ID: 31503492
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

12 Experts available now in Live!

Get 1:1 Help Now