jonessaha
Chibi Collector
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
In this tutorial, we will going to create a program that can calculate your age using C# as the programming language.
So, now let's start this tutorial!
1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Calculate age.
2. Next, add one DateTimePicker named DateTimePicker1 to have input of our dates. Add one TextBox named TextBox1 that will serve as the display of our age. Add also a Button named Button1 so that we can use this calculating our age. Design your interface like this one below:
3. Put this code for the Button1_Click. This will trigger to calculate your age.
We will initialized yr variable as integer to have the year difference between the present year and the year you were born as we code this syntax:
We initialized yr to have this month difference between the present month and the month you were born and we find its remainder as code Mod 12 because it has 12 Months as we code this syntax:
Next, we put this code below to display your age in the Textbox.
Full source code:
Output:
Download the source code below and try it! :)
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Download
So, now let's start this tutorial!
1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Calculate age.
2. Next, add one DateTimePicker named DateTimePicker1 to have input of our dates. Add one TextBox named TextBox1 that will serve as the display of our age. Add also a Button named Button1 so that we can use this calculating our age. Design your interface like this one below:

3. Put this code for the Button1_Click. This will trigger to calculate your age.
We will initialized yr variable as integer to have the year difference between the present year and the year you were born as we code this syntax:
- int
yr =
(
int
)
(
DateAndTime.
DateDiff
(
DateInterval.
Year
, DateTimePicker1.
Value
, DateTime.
Now
, (
Microsoft.
VisualBasic
.
FirstDayOfWeek
)
Microsoft.
VisualBasic
.
FirstDayOfWeek
.
Sunday
, (
Microsoft.
VisualBasic
.
FirstWeekOfYear
)
Microsoft.
VisualBasic
.
FirstWeekOfYear
.
Jan1
)
)
;
We initialized yr to have this month difference between the present month and the month you were born and we find its remainder as code Mod 12 because it has 12 Months as we code this syntax:
- int
month =
System
.
Convert
.
ToInt32
(
DateAndTime.
DateDiff
(
DateInterval.
Month
, DateTimePicker1.
Value
, DateTime.
Now
, (
Microsoft.
VisualBasic
.
FirstDayOfWeek
)
Microsoft.
VisualBasic
.
FirstDayOfWeek
.
Sunday
, (
Microsoft.
VisualBasic
.
FirstWeekOfYear
)
Microsoft.
VisualBasic
.
FirstWeekOfYear
.
Jan1
)
%
12
)
;
Next, we put this code below to display your age in the Textbox.
- TextBox1.
Text
=
yr.
ToString
(
)
+
" Years, "
+
month.
ToString
(
)
+
" Months "
;
Full source code:
- using
System.Diagnostics
;
- using
System
;
- using
System.Windows.Forms
;
- using
System.Collections
;
- using
System.Drawing
;
- using
Microsoft.VisualBasic
;
- using
System.Data
;
- using
System.Collections.Generic
;
- namespace
Calculate_Age
- {
- public
partial
class
Form1
- {
- public
Form1(
)
- {
- InitializeComponent(
)
;
- }
- public
void
Button1_Click(
System
.
Object
sender, System
.
EventArgs
e)
- {
- int
yr =
(
int
)
(
DateAndTime.
DateDiff
(
DateInterval.
Year
, DateTimePicker1.
Value
, DateTime.
Now
, (
Microsoft.
VisualBasic
.
FirstDayOfWeek
)
Microsoft.
VisualBasic
.
FirstDayOfWeek
.
Sunday
, (
Microsoft.
VisualBasic
.
FirstWeekOfYear
)
Microsoft.
VisualBasic
.
FirstWeekOfYear
.
Jan1
)
)
;
- int
month =
System
.
Convert
.
ToInt32
(
DateAndTime.
DateDiff
(
DateInterval.
Month
, DateTimePicker1.
Value
, DateTime.
Now
, (
Microsoft.
VisualBasic
.
FirstDayOfWeek
)
Microsoft.
VisualBasic
.
FirstDayOfWeek
.
Sunday
, (
Microsoft.
VisualBasic
.
FirstWeekOfYear
)
Microsoft.
VisualBasic
.
FirstWeekOfYear
.
Jan1
)
%
12
)
;
- // Dim day As Integer = DateDiff(DateInterval.Day, DateTimePicker1.Value, Now) Mod 30 - 10
- TextBox1.
Text
=
yr.
ToString
(
)
+
" Years, "
+
month.
ToString
(
)
+
" Months "
;
- }
- }
- }
Output:

Download the source code below and try it! :)
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Download
You must upgrade your account or reply in the thread to view hidden text.