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
  • 13,624
  • 2
  • 51
  • 62
Dhaval Panchal
  • 592
  • 3
  • 11
  • 31

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
  • 8,795
  • 2
  • 37
  • 60
  • 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