Gets the age based on Birthday. - CSharp System

CSharp examples for System:DateTime Day

Description

Gets the age based on Birthday.

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using System;/*  w  w w.jav  a 2 s  . co  m*/

public class Main{
        /// <summary>
        /// Gets the age based on Birthday.
        /// </summary>
        /// <param name="birthday">The birth date.</param>
        /// <returns>A integer for age.</returns>
        public static int GetAge ( this DateTime birthday )
        {
            var today = DateTime.Today;

            var age = today.Year - birthday.Year;
            if (today.Month < birthday.Month)
            {
                age = age - 1;
            }
            else if (today.Month == birthday.Month)
            {
                if (today.Day < birthday.Day)
                {
                    age = age - 1;
                }
            }

            return age;
        }
}

Related Tutorials