Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi every body how to get the dates of next month for 10 months or any number of months i try that but only gets me 6 months

what is the correct syntactics

What I have tried:

C#
var x = 10; // 10months
          for (int i = 0; i < x; i++)
          {
              DateTime sixmonthsfromnow = today.AddMonths(x);
              MessageBox.Show(sixmonthsfromnow.ToShortDateString());
              x--;
          }
Posted
Updated 7-Oct-16 4:32am
Comments
Karthik_Mahalingam 7-Oct-16 9:54am    
what is your expected output ?

Why are you changing the value of x inside the for loop? The worst thing you could possibly do in a for loop is to change the value of either the iterator (i) or the constant (x) INSIDE the loop.

Are you actually wanting to do this?

C#
StringBuilder str = new StringBuilder();
var x = 10; // 10months
for (int i = 0; i < x; i++)
{
    DateTime sixmonthsfromnow = DateTime.Now.AddMonths(i+6);
    str.AppendLine(sixmonthsfromnow.ToString("yyyy MM dd"));
}
MessageBox.Show(str.ToString());


EDIT =============

Whoever voted this a 1 - post your own solution if you think mine is so bad. FFS.
 
Share this answer
 
v4
Comments
Karthik_Mahalingam 7-Oct-16 9:54am    
5, based on the variable name, your logic is perfect.
MR.alaa 7-Oct-16 10:25am    
yes it gets me only 5 month dates i need ten month dates
Karthik_Mahalingam 7-Oct-16 10:29am    
if x is 10 then what is your expected output ?
thanks for John Simmons / outlaw That's i need


C#
StringBuilder str = new StringBuilder();
          var x = 10; // 10 months total period
          var y = 1; // variable for increase month every one,two ... month
          for (int i = 0; i < x; i++)
          {
              DateTime sixmonthsfromnow = DateTime.Now.AddMonths(i + y);
              str.AppendLine(sixmonthsfromnow.ToString("yyyy/MM/dd"));
          }
          MessageBox.Show(str.ToString());
 
Share this answer
 
v3
Comments
Richard MacCutchan 7-Oct-16 10:45am    
Why have you added another variable when you already have i counting up from 0 to 9? If you want it to count from 1 to 10 then adjust your for statement.

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