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
C D 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 C D 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

help
In excel, how do you calculate the number of months that have passed between the first of the year and the one date date in a field on my worksheet, I want a formula to automatically display the number of months that have passed since the first of this year and the date I entered. Everything I see is based on the number of months between two dates. I only have one. I appreciate your help! Jason Excel in Excel, for help look here http: / / www.cpearson.com / excel / datedif.htm Mike keywords: calculate, number, of, months, based, on, one, date description: In excel, how do you calculate the
Hi, I´m trying to calculate number of occurencies of a specific number, I´m using data from different sheets Formula: = NUMBER.IF
hi, Can someone please help me with a formula to calculate number of days, excluding weekend days? A1 B1 C1 Start date End date Total days (excluding weekend ld e en day(D2) Weekday(D1, 2) < 6) te, _ tDate) = 3D 1) keywords: calculate, number, of, days, formula description: hi, Can someone please help me with a formula to calculate
How to calculate number of days for 10 months, for generating report? HI Shanmugam Here we have need two date 2005); TimeSpan diff = now - then; int days = diff.Days; Thanks You can simply get the number of days for ten months by subtracting the two dates after converting then to Double ts.Minutes.ToString(); txtSeconds.Text = ts.Seconds.ToString(); txtMilliseconds.Text = ts.Milliseconds.ToString(); hi, To calculate the number of days between two dates in Asp.Net using C#, use the below
Hi to master gurus in excel, I really need your help to solve one issue: how to calculate number of collor cells? For instance, from cells range A2:F2 (6 cells), if there are 2 Excel SalesAid Software dguillett1@austin.rr.com Select your range and this will display the number of cells without color: Sub ColorMeElmo() Dim r As Range, Kount As Long Kount = 0 Sub Hope this helps! If so, click "YES" below. - - Cheers, Ryan keywords: How, do, I, calculate, number, of, colored, cells? description: Hi to master gurus in excel, I really need your
How do I calculate the number of weeks between two cells? Example B2 = 1 / 1 / 08 B3 = 5 / 10 / 09 B4 in Excel and find differences with Workbook Compare A free, powerful, flexible Excel utility keywords: Calculate, number, of, weeks, between, two, cells description: How do I calculate the number of weeks between two
Hi I want to be able to use a formula to calculate the number of days in a month based on a date in question - e.g. in A1 of the previous month. - - Dave Peterson = DAY(EOMONTH(A1, 0)) Required ATP add-ins keywords: Calculate, Number, of, Days, in, a, Month description: Hi I want to be able to use a formula
calculate Number of weeks between two dates in sql Number of weeks between two dates in sql You get number of week between two date using datediff function SELECT DATEDIFF( week, date_1, date_2) FROM title Number of weeks between two dates in sql ( 1330 Views ) keywords: SELECT description calculate Number of weeks between two dates in sql
Excel Worksheet Discussions Days (1) NOTHING! But, based upon the subject of your post, try "Calculate the difference between two dates" in the help file. Next time, post your question in the body. Nevermind! I see your new thread. keywords: how, can, I, calculate, number, of, days, based, on, start, and, end, dates?
Hi , I have an excel sheet. i want to calculate the number of rows in the excel sheet using Vb6. This is the code that i have rlmt As Integer Open CommonDialog1.FileName For Input As #1 rlmt = 0 ' To get the number of rows While Not EOF(1) Line Input #1, S rlmt = rlmt + 1 Wend Close Anu, The function you are using is only suited for textfiles. If you want the number of rows in an excel sheet, use Sheet1.UsedRange.Rows.Count() but remember that for structure of the worksheet(s). http: / / support.microsoft.com / kb / 257819 NickHK keywords: How, to, calculate, Number, of, rows, in, Excel, ? description: Hi , I have an excel sheet. i want to