Calculating the Number of Years Interval Between Two Dates Using C#
Submitted by janobe on Friday, November 23, 2018 - 21:41.
In this tutorial, I will teach you how to calculate the number of years interval between two dates using C#. This simple program is commonly used for getting the age of the person or getting if how many years you have been working in a company. Follow the procedure below to see how it works
For more question about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
FB Account – https://www.facebook.com/onnaj.soicalap
Or feel free to comment below.
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create new windows for application for C#.
Step 2
Do the form just like this.
Step 3
Go to the code editor and create a function to get the number of years interval between two dates.- private static int calculateYears(DateTime datefrom, DateTime dateto)
- {
- int int_year;
- int_year = dateto.Year - datefrom.Year;
- return int_year;
- }
Step 4
Double click the button and do the following codes for getting the number of years interval between two dates and display it inside the listbox when the button is clicked.- private void button1_Click(object sender, EventArgs e)
- {
- listBox1.Items .Clear();
- int result_year = calculateYears(dateTimePicker1.Value, dateTimePicker2.Value);
- listBox1.Items.Add("Result :" + result_year + " Year(s)");
- }
Add new comment
- 276 views