locked
Date 5 days from now RRS feed

  • Question

  • User60581005 posted

     Hello,

    I am trying to get the date 5 days from now.
    Example: today = 7/11/2007, date = 7/16/2007 OR today = 12/30/2007, date = 1/4/2008.

    A bonus would be to not count weekends, ie only count business days monday - friday.
    Example: today = 7/11/2007, date = 7/18/2007

    Thanks a bunch,
     

    Wednesday, July 11, 2007 9:23 AM

Answers

  • User541108374 posted

    Hi,

    if you have a DateTime available you can use the AddDays(5) method on it.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2007 9:26 AM
  • User-989915499 posted

    Hi there,

    A bonus would be to not count weekends, ie only count business days monday - friday.
    Example: today = 7/11/2007, date = 7/18/2007

    Not sure there is any built-in feature to do that, anyway i ahve this workaround hope this is help

    Dim mydate As DateTime = DateTime.Now ' You can set your selected datetime hereFor i As Integer = 1 To 5

    mydate = mydate.AddDays(1)

    If mydate.DayOfWeek() = DayOfWeek.Saturday Or mydate.DayOfWeek = DayOfWeek.Sunday Then

    i -= 1 'minus current count of day is saturday or sunday

    End If

    Next

    Response.Write(mydate.ToString())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2007 9:38 AM
  • User519246680 posted

    The easy answer is:

    DateTime.Now.AddDays(5);

    That will get you a date that is exactly 5 days from now.

    In this example, I'm assuming we have a calendar control, and the user has selected a date.  For a specified date, you would use:

    DateTime myDate = Calendar1.SelectedDate;
    myDate.AddDays(5);

    In order to exclude weekends, you would need to write a function.  Something like this:

    DateTime myDate = Calendar1.SelectedDate;
    myDate.AddDays(ExcludeWeekends(myDate, 5));

    private int ExcludeWeekends(DateTime date, int days)
    {
        int daysWithSkip = days;
       
    for (int i = 0; i < days; i++)
        {
           
    if ((date.AddDays(i).DayOfWeek == DayOfWeek.Saturday) || (date.AddDays(i).DayOfWeek == DayOfWeek.Sunday))
            {
                daysWithSkip += 1;
            }
        }

        return daysWithSkip;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2007 9:39 AM
  • User60581005 posted

    Ah, I didn't notice the "AddDays()" function.  That helped Immensely.

    Here's what I did to exclude weekends.  I loop through days excluding saturday and sunday until my count equals the # of days I want.  That way I can give any amount of days in advance.

     

    1        Sub Page_Load(s As Object, e As EventArgs)
    2            If Not IsPostBack Then
    3                Dim exDate As Date = FutureDate(4)
    4                    
    5                txtexDate.Text = Format(exDate, "M/d/yyyy")
    6            End If
    7        End Sub
    8        
    9        
    10       Function FutureDate(i As Integer)
    11           Dim myDate As Date
    12           Dim c As Integer = 1
    13           Dim count As Integer = 0
    14           Do While count < i
    15               If Now.AddDays(c).DayOfWeek <> DayOfWeek.Saturday And Now.AddDays(c).DayOfWeek <> DayOfWeek.Sunday Then
    16                   count = count + 1
    17               End If
    18               c = c + 1
    19           Loop
    20           
    21           myDate = Now.AddDays(c)
    22           
    23           Return myDate
    24       End Function
    
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2007 12:51 PM

All replies

  • User541108374 posted

    Hi,

    if you have a DateTime available you can use the AddDays(5) method on it.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2007 9:26 AM
  • User-989915499 posted

    Hi there,

    A bonus would be to not count weekends, ie only count business days monday - friday.
    Example: today = 7/11/2007, date = 7/18/2007

    Not sure there is any built-in feature to do that, anyway i ahve this workaround hope this is help

    Dim mydate As DateTime = DateTime.Now ' You can set your selected datetime hereFor i As Integer = 1 To 5

    mydate = mydate.AddDays(1)

    If mydate.DayOfWeek() = DayOfWeek.Saturday Or mydate.DayOfWeek = DayOfWeek.Sunday Then

    i -= 1 'minus current count of day is saturday or sunday

    End If

    Next

    Response.Write(mydate.ToString())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2007 9:38 AM
  • User519246680 posted

    The easy answer is:

    DateTime.Now.AddDays(5);

    That will get you a date that is exactly 5 days from now.

    In this example, I'm assuming we have a calendar control, and the user has selected a date.  For a specified date, you would use:

    DateTime myDate = Calendar1.SelectedDate;
    myDate.AddDays(5);

    In order to exclude weekends, you would need to write a function.  Something like this:

    DateTime myDate = Calendar1.SelectedDate;
    myDate.AddDays(ExcludeWeekends(myDate, 5));

    private int ExcludeWeekends(DateTime date, int days)
    {
        int daysWithSkip = days;
       
    for (int i = 0; i < days; i++)
        {
           
    if ((date.AddDays(i).DayOfWeek == DayOfWeek.Saturday) || (date.AddDays(i).DayOfWeek == DayOfWeek.Sunday))
            {
                daysWithSkip += 1;
            }
        }

        return daysWithSkip;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2007 9:39 AM
  • User1312102753 posted

     hi,

    if you always add 7 days then you will skip weekends, add 6 days on sat. and add 5 on sunday like so...

            Dim dtTest As DateTime
            dtTest = DateTime.Now
            If dtTest.DayOfWeek = DayOfWeek.Saturday Then
                dtTest = dtTest.AddDays(6)
            ElseIf dtTest.DayOfWeek = DayOfWeek.Sunday Then
                dtTest = dtTest.AddDays(5)
            Else
                dtTest = dtTest.AddDays(7)
            End If



            txtDate.Text = dtTest

     

    hope that helps,

    Nameth

    Wednesday, July 11, 2007 9:46 AM
  • User1582134116 posted

     

    You do this

    DateTime PDate=DateTime.Today.AddDays(5);
    		string nDate="";
    		for(int i=0;i<7;i++)
    			{
    			if (PDate.AddDays(i).DayOfWeek==DayOfWeek.Saturday) 
    			{
    				nDate=DateTime.Today.AddDays(6).ToShortDateString();
    			}
    			else if	(PDate.AddDays(i).DayOfWeek==DayOfWeek.Sunday)
    			{
    				nDate=DateTime.Today.AddDays(7).ToShortDateString();
    			}
    			else
    			{
    				nDate=DateTime.Today.AddDays(5).ToShortDateString();
    			} 
    			}
    string newDate=nDate;
    TextBox2.Text=nDate;
    
    I have stored the value in a Text Box as well as a variable newDate.
     
    Wednesday, July 11, 2007 10:57 AM
  • User60581005 posted

    Ah, I didn't notice the "AddDays()" function.  That helped Immensely.

    Here's what I did to exclude weekends.  I loop through days excluding saturday and sunday until my count equals the # of days I want.  That way I can give any amount of days in advance.

     

    1        Sub Page_Load(s As Object, e As EventArgs)
    2            If Not IsPostBack Then
    3                Dim exDate As Date = FutureDate(4)
    4                    
    5                txtexDate.Text = Format(exDate, "M/d/yyyy")
    6            End If
    7        End Sub
    8        
    9        
    10       Function FutureDate(i As Integer)
    11           Dim myDate As Date
    12           Dim c As Integer = 1
    13           Dim count As Integer = 0
    14           Do While count < i
    15               If Now.AddDays(c).DayOfWeek <> DayOfWeek.Saturday And Now.AddDays(c).DayOfWeek <> DayOfWeek.Sunday Then
    16                   count = count + 1
    17               End If
    18               c = c + 1
    19           Loop
    20           
    21           myDate = Now.AddDays(c)
    22           
    23           Return myDate
    24       End Function
    
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2007 12:51 PM
  • User-989915499 posted

    Hi There,

    Is it something wrong with my solution?

    What i see you are doing is actually similar what what i am doing.

    You are <> and + 1

        If Now.AddDays(c).DayOfWeek <> DayOfWeek.Saturday And Now.AddDays(c).DayOfWeek <> DayOfWeek.Sunday Then
    16                   count = count + 1
    17               End If

    im using = and - 1

    If myDate.DayOfWeek = DayOfWeek.Saturday Or myDate.DayOfWeek = DayOfWeek.Sunday Then

    i -= 1

    End If

    Wednesday, July 11, 2007 5:11 PM
  • User1312102753 posted

     haha who cares man he got his problem solved, nothing is wrong with your solution.

    Wednesday, July 11, 2007 5:28 PM
  • User-989915499 posted

    Hi There,

    Even though no one will care, but i will care.

    The purpose im here is helping people as well as sharpen my own skill.

    If the solution that i given is not working, i still want to know the reason and improve there.

    If i don't care, i don't see i point im here anymore.

    I think many people think the same too or just me.

    Wednesday, July 11, 2007 7:16 PM
  • User1312102753 posted

    well hello again,

    sry not trying to be an a-hole but you just came across like you wanted some credit for it.. and i said who cares because he solved his problem.  I Looked at your solution and yeah it would have worked fine, thats why i said "Nothing is wrong with your solution" and you should know very well yourself if it worked or not as the question as well as the answer was not a hard one. Good Day.

    I SAID GOOD DAY!


    Thursday, July 12, 2007 2:09 AM
  • User-989915499 posted

    THIS IS ANOTHER REASON TOO!

    "THIS IS WHOLE WHO IDEA OF HAVING THE POINT SYSTEM. IF NOT, WHY IS IT IN H*LL THIS FORUM WANT TO HAVE POINT SYSTEM."

    THE POINT SYSTEM IN A WAY ENCOURAGE, MOTIVATE PEOPLE ACTIVELY PARTICIPATE IN THIS FORUM.

    BE SINCERE, ANSWERER WOULD EXPECT A CREDIT IF THE ANSWER THEY PROVIDE IS CORRECT.

    GOOD DAY

     

    Thursday, July 12, 2007 2:22 AM
  • User541108374 posted

    Hi,

    I marked your original answer also. 

    THE POINT SYSTEM IN A WAY ENCOURAGE, MOTIVATE PEOPLE ACTIVELY PARTICIPATE IN THIS FORUM.

    You're correct on that part.

    I would like to suggest that if you want to further debate on the meaning of recognition points to do so in the Feedback forum where you'll see more debates concerning this, instead of going further and making this thread a hopeless mess with non related replies.

    Grz, Kris.

    Thursday, July 12, 2007 7:20 AM
  • User476156134 posted

    This is a fantastic solution, but has one minor bug.

    for (int i = 0; i < days; i++) 

        should be changed to

    for (int i = 0; i < daysWithSkip; i++)

    Or it may actually return a weekend day on you.  I.E. Say it's Wednesday and you call "ExcludeWeekends(todaysDate,3)" it will return a Saturday, because the loop stops after it reaches days, even though you are on a weekend.  Thanks for writing the method, this helped me a lot.  Just thought I would share one minor change. :)

     -Renée

     

    Wednesday, August 8, 2007 7:16 PM