Add to favorites list with last 5 years

How can I create a list for the last 5 years, for example, years 2011before 2007. I don’t want to hard code these years, but I want the last 5 years to be based on this year.

+5
source share
3 answers

Put the last 5 years in your view model and bind to it:

var last5Years = from n in Enumerable.Range(0,5)
                 select DateTime.Now.Year - n;
+10
source

DateTime.Now.Year will give you the current year, then you can use a loop

DateTime dt = DateTime.Now;
for(int i = 0; i < 5; i++)
   list.Add(dt.Now.Year - i);
0
source

Something like that:

List<int> last5Years = new List<int>();
int currentYear = DateTime.Now.Year;
for (int i = currentYear - 5; i < currentYear; i++)
{
    last5Years.Add(i);
}
//databind here
0
source

All Articles