-7

I'm really stuck with calculating date of birth given a persons age in months. Currently, I'm using C# to do that. However, failed to understand the logic. Thanks in advance

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
aby
  • 810
  • 6
  • 21
  • 36
  • 5
    Can you post the logic that you did not understand? And what is the area where you have a problem? – Ravi Y Dec 26 '12 at 09:01
  • Like if I'm provided a persons age in years. I can simply subtract it from the current year and assign the current date and month as part of the date of birth - that's what my project team accepted! But, the same logic could not be applied for age in terms of Month. Eg. someones' age is 18 months. – aby Dec 26 '12 at 09:07
  • So what prevents you from subtracting months from a given DateTime object? – Ravi Y Dec 26 '12 at 09:11

1 Answers1

2

DateTime has perfect method to substract (actually add negative value) months from current date. For example:

int ageInMonths = 18;
DateTime dt = DateTime.Today;
DateTime DateOfBirth = dt.AddMonths(-ageInMonths);
Nogard
  • 1,779
  • 2
  • 18
  • 21