• We just launched and are currently in beta. Join us as we build and grow the community.

Age Calculator Using C#

jonessaha

Chibi Collector
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
51
Likes
191
Bits
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:

agedesign_0.png


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:
  1. 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:
  1. 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.
  1. TextBox1.

    Text

    =

    yr.

    ToString

    (

    )

    +

    " Years, "

    +

    month.

    ToString

    (

    )

    +

    " Months "

    ;


Full source code:

  1. using

    System.Diagnostics

    ;
  2. using

    System

    ;
  3. using

    System.Windows.Forms

    ;
  4. using

    System.Collections

    ;
  5. using

    System.Drawing

    ;
  6. using

    Microsoft.VisualBasic

    ;
  7. using

    System.Data

    ;
  8. using

    System.Collections.Generic

    ;



  9. namespace

    Calculate_Age
  10. {
  11. public

    partial

    class

    Form1
  12. {
  13. public

    Form1(

    )
  14. {
  15. InitializeComponent(

    )

    ;


  16. }


  17. public

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  18. {
  19. 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

    )

    )

    ;
  20. 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

    )

    ;
  21. // Dim day As Integer = DateDiff(DateInterval.Day, DateTimePicker1.Value, Now) Mod 30 - 10
  22. TextBox1.

    Text

    =

    yr.

    ToString

    (

    )

    +

    " Years, "

    +

    month.

    ToString

    (

    )

    +

    " Months "

    ;
  23. }
  24. }

  25. }


Output:

ageout.png


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.
 

452,292

323,341

323,350

Top