Get Days Before Birthday - CSharp System

CSharp examples for System:DateTime Calculate

Description

Get Days Before Birthday

Demo Code


using System;//from   w  ww. j  a  v  a  2s.  com

public class Main{
        public static int GetDaysBeforeBirthday(DateTime birthDate)
        {
            var today = DateTime.Now;
            var nextBirthday = new DateTime(today.Year, birthDate.Month, birthDate.Day);
            TimeSpan difference = nextBirthday - DateTime.Now;
            if (difference.Days < 0)
            {
                nextBirthday = new DateTime(today.Year + 1, birthDate.Month, birthDate.Day);
                difference = nextBirthday - DateTime.Now;
            }
            return difference.Days;
        }
}

Related Tutorials