Scope
I wanted to calculate a person's age in .Net. I am a little disappointed there is no SQL-like DateDiff function in .Net Framework.
If you search on Google, you will find these with high rank and votes.
If you search on Google, you will find these with high rank and votes.
- http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age
- http://stackoverflow.com/questions/673476/age-in-years-from-datetime-date-of-birth
- http://stackoverflow.com/questions/3152977/calculate-the-difference-between-two-dates-and-get-the-value-in-years
- http://stackoverflow.com/questions/4129356/calculating-difference-between-two-datetime-objects-in-c-sharp
- etc...
Solution
If someone ask me how old am I, it is simple logical process.
- What year am I in?
- What year did I born?
- And have I had my birthday this year yet?
public int? Age { get { return (DateTime.Today.DayOfYear >= DOB.DayOfYear) ? (DateTime.Today.Year - DOB.Year) : (DateTime.Today.Year - DOB.Year - 1); } }
No comments:
Post a Comment