I needed to create a dropdown list that had all the years back to a certain date. To accomplish this I would need to calculate difference between years back to the specified date. This isn’t really a difficult but here is the code just incase anyone else needs to do the same.
Here is how I did it:
DateTime dtStartYear = new DateTime(2010, 1, 1);
DateTime dtThisYear = DateTime.Now;
int iYearDiff = (dtThisYear.Year - dtStartYear.Year);
for(int i = 0; i <=iYearDiff; i++)
string strTemp = DateTime.Now.AddYears(-i).Year.ToString();
ListItem li = new ListItem(strTemp, strTemp);
Pretty simple right? Not sure if there is a better way, but this seems to give me what I needed.