Advertisement
jyoung12387

Calculate age based on birthday

Feb 25th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.50 KB | None | 0 0
  1. using System;
  2.                    
  3. public class Program
  4. {
  5.     public static void Main()
  6.     {
  7.         //get todays date and the birthdate
  8.         DateTime today = DateTime.Today;
  9.         DateTime birthday = new DateTime(1987,1,23);
  10.        
  11.         //calc difference in years
  12.         int age = today.Year - birthday.Year;
  13.        
  14.         //Check to see if birthday has happened yet. If not adjust age down by 1
  15.         if (birthday.DayOfYear > today.DayOfYear)
  16.             age = age -1;
  17.        
  18.         //Display age on Console
  19.         Console.WriteLine("Your current age is {0}.", age);
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement