Thursday, August 30, 2012

Get your Age



 protected int getAge()
        {
            TimeSpan t = Convert.ToDateTime(DateTime.Now) - Convert.ToDateTime(("10/19/1977"));
            int leapdays = getLeapDays(Convert.ToDateTime(("10/19/1977")), Convert.ToDateTime(DateTime.Now));
            int Totaldays = Convert.ToInt32(t.TotalDays - leapdays);
            int yr = Totaldays / 365;
            if (yr <= 0)
                yr = 1;
            return yr;
        }

        private int getLeapDays(DateTime startDate, DateTime endDate)
        {
            int leapdays = 0;
            while (startDate < endDate)
            {
                if ((Convert.ToInt32(startDate.Year) % 4) == 0)
                    leapdays++;
                startDate = startDate.AddYears(1);
            }
            return leapdays;
        }

Page Load:

  int age = getAge();
  Response.Write(age);


1 comment: