0

I want to calculate the age based on birth date and it should rounded to the nearest i.e. 19/03/1988 would be resultant into 26 and 19/09/1988 to 25.

Below is the current implementation.

var ts = DateTime.Now - dtBirthdate;
var age = ts.Days / 365;
Gusdor
  • 14,001
  • 2
  • 52
  • 64
Dhaval Panchal
  • 612
  • 4
  • 12
  • 32

1 Answers1

3

The problem with your current implementation is integer division. If you replace with a double, it should work better:

var ts = DateTime.Now - new DateTime(1988, 3, 19);
var age = Math.Round(ts.Days / 365.0);
madd0
  • 9,053
  • 3
  • 35
  • 62
  • Thank you! It worked for me! The link provided in above comment by Andrew Kim is not working too! I don't know how without knowing people has started down voting this question. – Dhaval Panchal Dec 03 '13 at 10:05