Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi
I have a starting date, now i need to make a loop to count for 5 days from the start date. The output should show the dates for the next 5 days. Please help.
Posted
Comments
Tejas Vaishnav 9-Sep-11 9:07am    
If you Like My Answer then rate it please....
Anele Ngqandu 12-Sep-11 6:13am    
haha...ok sir

C#
void BindDates()
  {
  for (int i=0; i<5;i++)
  {

      DateTime date=DateTime.Now.AddDays(i);
      Response.Write(date.ToString());

  }
  }
 
Share this answer
 
C#
//Set the Start Date
DateTime dtStart = DateTime.Now.Date

//Get the date 5 days greater than start date
DateTime dtEnd = dtStart.AddDays(5)

//Set the looping parameters
DateTime dtLoop = dtStart

//Apply while loop
While (dtLoop < dtEnd)
{
    //Do something with dtLoop here or Print the date value
    Response.Write(dtloop.ToString());

    //Add one dat in the start value
    dtLoop = dtLoop.AddDays(1)
}


Hope this helps.
All the best.
 
Share this answer
 
v2
Comments
Anele Ngqandu 9-Sep-11 7:54am    
Greate solution sir, Now this is what i did with it

DateTime dtStart =Convert.ToDateTime( DatePicker.Text);


DateTime dtEnd = dtStart.AddDays(5);


DateTime dtLoop = dtStart;


while(dtLoop < dtEnd)
{
//Do something with dtLoop here or Print the date value
MessageBox.Show(dtLoop.ToString());

//Add one dat in the start value
dtLoop = dtLoop.AddDays(1);
}

Since the date will be changing i decided to put DatePicker.Text and now its not working. Any Ideas to solve this
Pravin Patil, Mumbai 10-Sep-11 10:05am    
I am glad that my solution helped you. What error are you getting..?
I think DateTime dtStart =Convert.ToDateTime( DatePicker.Text); should be replaced with
DateTime dtStart =Convert.ToDateTime(DatePicker.Value);
C#
protected void GenrationDate()
       {
           DateTime dtStart = DateTime.Now.Date;

           //Get the date 5 days greater than start date
           DateTime dtEnd = dtStart.AddDays(5);

           //Set the looping parameters
           DateTime dtLoop = dtStart;
           int i = 0;
           for (i = dtStart.Day; i < dtEnd.Day; i++)
           {
               //Printing the Date as short formate
               Response.Write(dtLoop.ToShortDateString() + "<br />");
               //adding one day to date
               dtLoop = dtLoop.AddDays(1);
           }
       }


hope this will work....
 
Share this answer
 
C#
System.DateTime dt = new System.DateTime(2011, 9, 9);
for (i = 1; i <= 5; i++) {
    //dt.AddDays(i) use
}
 
Share this answer
 
v2
Comments
Anele Ngqandu 9-Sep-11 7:26am    
No sir you counting 5 days, I need the dates for the 5 days
Mohd Wasif 9-Sep-11 7:31am    
Please clarify bit about output
Anele Ngqandu 9-Sep-11 7:40am    
starting date=9/9/2011
output=9/10/2011
9/11/2011
9/12/2011

9/13/2011
C#
DateTime Dt = DateTime.Now.Date;
          for (int ik = 0; ik < 5; ik++)
          {

              MessageBox.Show(Dt.AddDays(Convert.ToDouble(ik)).ToString ());

          }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900