Solved

Get all years between TWO years returned as a array ?

Posted on 2009-04-01
8
165 Views
Last Modified: 2012-05-06
Hello,

I have this:

startYear = 2005
todaysYear = 2009

Now I want all years between those 2 dates , this would be 2006,2007,2008 returned in a array does there exist already such a function?

I want to iterate in a for loop through that array and get access to every single year and do some stuff with it.
0
Comment
Question by:MSFanboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24040504
jsut create a function and loop in that for the start year to the end year and add the years found in a List<int> object
you will get a int list and you can do whatever you want to do that

i dont think there is a function which will give you the year values between 2 dates
yes you can get the count of no. of years from 2 dates
0
 
LVL 4

Accepted Solution

by:
amazingwolf1 earned 250 total points
ID: 24040647
here it is, call it like this:

int[] res = GetYears(2009, 2005);
public int[] GetYears(int iUpper, int iLower)
    {
        int iGap;
        int[] resultArray;
 
        iGap = iUpper - iLower - 1;
 
        // check if this is the same year
        if (iGap <= 0)
        {
            resultArray = new int[1];
            resultArray[0] = iUpper;
        }
        else
        {
            resultArray = new int[iGap];
            for (int i = iLower + 1, index = 0; i < iUpper; i++, index++)
            {
                resultArray[index] = i;
            }
        }
 
        return resultArray;
    }

Open in new window

0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 24040775
Not sure why you need an array...couldn't you just use your year values directly in a for loop?

            int startYear = 2005;
            int stopYear = 2009;
            for (int year = startYear; year <= stopYear; year++)
            {
                Console.WriteLine(year.ToString());
            }

But if you really want an Array, I like ragi's suggestion:
(one your your PAQs dealt w/ VS2008 so I'm pretty sure you have List<T>)
(furthermore, instead of an Array, why not just deal with the List directly?)
        private void button1_Click(object sender, EventArgs e)
        {
            int[] years = GetYears(2005, 2009);
            foreach (int year in years)
            {
                Console.WriteLine(year.ToString());
            }
        }
 
        private int[] GetYears(int startYear, int endYear)
        {
            List<int> years = new List<int>();
            for (int y = startYear; y <= endYear; y++)
            {
                years.Add(y);
            }
            return years.ToArray();
        }

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:MSFanboy
ID: 24041976
Hm seems the info I gave u was not 100% from my side:

The user inputs e.g. "X" what means X years back into the past. I wanted a native method for it but it does not exist so I had to write my own:

You should NOT take todayYear = 2009 literally it can be also 2010,2200 or 25000 ... so I ca not guess this date just get DateTime.Now and do stuff on it



foreach (var s in YearsList(6))
            {
                    Console.WriteLine("Jahr: " + s);
            }
 
public string[] YearsList(int value)
        {
            var yearsArray = new string[value];
            for (int i = 0; i < value; i++)
            {
                yearsArray[i] = DateTime.Now.AddYears(-i).ToString("yyyy");
            }
 
            return yearsArray;
        }

Open in new window

0
 
LVL 4

Expert Comment

by:amazingwolf1
ID: 24047497
Since the original questions was not clear enough, and MSFanboy did not state clearly that he ONLY wants a native function. I gave him a full solution here with the code, and I feel it would be an error to approve to MSFanboy request.
0
 
LVL 4

Expert Comment

by:amazingwolf1
ID: 24071733
I recommend #3 - Accept one or more Expert posts as the answer. I suggest posts
http:#a24040647, http:#a24040775, http:#a24041976 will be the answers.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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