Solved

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

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

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 58
Function after success of Jquery/AJAX action 9 29
How to read text with RegEx... 2 26
Finding the IIS version 5 22
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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:…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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