[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

How to program a var to iterate throgh the values...

I need a var to iterate through:

DateTime.Now.ToString("MM"), down 6 months.

If now is january I need:
"01"
"12"
...
"07"


0
Rahn
Asked:
Rahn
1 Solution
 
infochandruCommented:
Hope this will help

        for (int i = 0; i < 7; i++)
        {
            lblTest.Text += DateTime.Now.AddMonths(-i).ToString("MM") + "<br>";
        }
0
 
zadeveloperCommented:

var StartDate = DateTime.Now;
            for (var i = 0; i < 6; i++)
            {
                System.Diagnostics.Debug.Print(StartDate.AddMonths(-1*i).ToString("MM"));
            }

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
Meir RivkinFull stack Software EngineerCommented:
check this code snippet.
the function get 3 params:
1 - inital month (from DateTime.Now.ToString("MM"))
2 - number of months to subtract in each iteration
3 - number of iterations
static void Main(string[] args)
        {
            string str = DateTime.Now.ToString("MM");
            int resMonth = IterateDownMonth(int.Parse(str), 7, 8);
        }

        static int IterateDownMonth(int initMonth, int subMonths, int iterations)
        {
            while (iterations-- > 0)
            {
                initMonth -= subMonths;
                initMonth = initMonth > 0 ? initMonth : initMonth + 12;
            }

            return initMonth;
        }

Open in new window

0
 
Roshan DavisCommented:

DateTime dt = DateTime.Now
for(int i = 0 ; i <6; i++)
{
       string strDisplay = dt.AddMonths(-i).ToString("MM");
}

Open in new window

0
 
zadeveloperCommented:
so pretty much all the same thing :)
0
 
RahnAuthor Commented:
this for loop presents:
01
12
10
7
3
....

   for (int i = 0; i < 7; i++)
        {
            lblTest.Text += DateTime.Now.AddMonths(-1).ToString("MM") + "
";
        }
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now