C# .NET - To calculate total number of years - Asked By abinav shankar on 07-Feb-12 09:08 AM

Earn up to 20 extra points for answering this tough question.

Hi

I have to calculate the total number of years (i.e) my date of joining is on 08-01-2009
i have to calculate the number of years till date and display it in the dropdown as follows 
2009
2010
2011
2012

Danasegarane Arunachalam replied to abinav shankar on 07-Feb-12 09:13 AM
Hello,

Please find the code

  DateTime dt = Convert.ToDateTime("12/01/2009");
        DateTime dtCurrentdate = DateTime.Now;
        int TotalYear = dtCurrentdate.Year - dt.Year;


This will find the no of years

Danasegarane Arunachalam replied to abinav shankar on 07-Feb-12 09:17 AM
And if you want the Starting year also, then use the below code




 DateTime dt = Convert.ToDateTime("12/01/2009");
        DateTime dtCurrentdate = DateTime.Now;
        int TotalYear = ((dtCurrentdate.Year) - (dt.Year-1));
     

This will find the no of years including current year
Danasegarane Arunachalam replied to abinav shankar on 07-Feb-12 09:21 AM
Display in dropdown


 DateTime dt = Convert.ToDateTime("12/01/2009");
        DateTime dtCurrentdate = DateTime.Now;
        int TotalYear = ((dtCurrentdate.Year) - (dt.Year-1));
        int year=0;
        //'And display it in the Dropdown
        year=dt.Year;
          for(int i=dt.Year;i<=dtCurrentdate.Year;i++)
          {
            
            comboBox1.Items.Add(year.ToString());
            year++;
          }
[)ia6l0 iii replied to abinav shankar on 07-Feb-12 09:23 AM
You can use the Enumerable.Range method too. 

int joiningYear = new DateTime(2009, 01, 08).Year;
var years = Enumerable.Range(joiningYear, DateTime.Now.Year - joiningYear + 1);

//And then bind the datasource to the dropdown.
ddlYears.DataSource =years;
ddlYears.DataBind();

Hope this helps.
Pat Hartman replied to abinav shankar on 07-Feb-12 11:33 AM
Your sample doesn't match your question.  The sample shows some year dates but you are asking how to calculate the difference between two dates in years.  I don't work with C# but I'm sure it has some date functions you could use.  In VBA we would use the Year() function to extract the year part from a date.  We would use the DateDiff() function to find the difference between two dates.  The function takes an argument that specifies the units you want your answer in - days, months, years, etc. 
print datediff("yyyy",#1/1/2009#, #2/28/2011#)
 2
print datediff("d",#1/1/2009#, #2/28/2011#)
 788
print datediff("m",#1/1/2009#, #2/28/2011#)
 25
print datediff("w",#1/1/2009#, #2/28/2011#)
 112
print Year(#2/28/2011#)
 2011
Chintan Vaghela replied to abinav shankar on 09-Feb-12 07:10 AM

Hello,

 

Bind your dropdown as following way

DateTime joiningDate = Convert.ToDateTime("08-01-2009");//Set Date string into DateTime Object

int JoiningYear = joiningDate.Year;//Get Joining Year

int countYear = DateTime.Now.Year - JoiningYear;//Take Year Diifference

// For loop for getting Date

for (int i = 0; i <= countYear; i++)

{

    drpYear.Items.Add(new ListItem(JoiningYear.ToString(), JoiningYear.ToString()));

    JoiningYear++;

}

 

Hope this is helpful !

Thanks

 

 

 

 

 

Pat Hartman replied to Chintan Vaghela on 09-Feb-12 05:43 PM
RB,
You can't find the difference in years between two dates by simply extracting the year parts and subtracting.  Should the difference between 12/31/2011 - 1/1/2012 really be 1 year?  If C# doesn't have the equivalent of a datediff() function, you should convert a date to a serial number and then subtract the serial numbers and divide by 365.  Access/Jet/ACE use Dec 30, 1899 as the origin date for their date/time data type (SQL server uses a different origin date and I believe Excel does also).  So any date after that is a positive number and any date prior is a negative number.
print cdbl(#2/9/2012#)
 40948
print cdbl(#2/2/1899#)
-331