Age Calculator which returns byte value - CSharp System

CSharp examples for System:DateTime Calculate

Description

Age Calculator which returns byte value

Demo Code


using System;/*from   w  w  w  .java  2s.c om*/

public class Main{
        public static byte? Age(DateTime? date)
        {
            if (!date.HasValue)
            {
                return null;
            }

            var birthDate = date.Value;
            var now = DateTime.Now;

            var age = now.Year - birthDate.Year;
            if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
            {
                age--;
            }

            return (byte)age;
        }
}

Related Tutorials