Solved

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

Posted on 2008-10-06
5
2,420 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 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