what is the simplest and correct way to calculate age?

Possible Duplicate:
How do I calculate someone's age in C#?

Hi, Simple question. Need to calculate age and only allow people who are 21. Do you use timespan? so given somebody inputting a date i need to check if they are >=21.

suggestions?


The easiest way isn't to actually calculate their age - it's to see whether their birthday is after your limit:

DateTime twentyOneYearsAgo = DateTime.Today.AddYears(-21);

if (birthDate > twentyOneYearsAgo)
{
    // Sorry, you're too young
}

Note that on February 29th on a leap year, the AddYears will return February 28th on the relevant earlier year. That's probably what you want in this case, if the user has entered an actual date.

链接地址: http://www.djcxy.com/p/54358.html

上一篇: 属性或索引器不能分配给

下一篇: 计算年龄的最简单和最正确的方法是什么?