Calculating difference between years
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:
1 2 3 4 5 6 7 8 9 10 11 | 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); ddlYear.Items.Add(li); } |
Pretty simple right? Not sure if there is a better way, but this seems to give me what I needed.