简体   繁体   English

给定字符串输入,如何在 C# 中计算年龄?

[英]Given string input, how do I calculate age in C#?

I want to calculate a person's present age.我想计算一个人现在的年龄。 The date of birth (DOB) data is entered using a TextBox and my database stores DOB as a varchar.出生日期 (DOB) 数据是使用TextBox输入的,我的数据库将 DOB 存储为 varchar。

How do I find the current age from this birth date?如何从这个出生日期找到当前年龄?

There are lots of things you need to do here.在这里您需要做很多事情。 First of all you need a way to parse the date stored in the database, originally typed in manually by a user.首先,您需要一种方法来解析存储在数据库中的日期,该日期最初是由用户手动输入的。 That is a difficult task, because users tend to type just about anything.这是一项艰巨的任务,因为用户几乎可以输入任何内容。

Start off with DateTime.Parse() , deal with all the errors you will get, and yes, you will get errors.DateTime.Parse() ,处理你会得到的所有错误,是的,你会得到错误。 How do you know, for instance, what "09/10/11" means?例如,您如何知道“09/10/11”是什么意思? Is this an American date or a British one?这是美国约会还是英国约会?

One possibility is to switch from TextBox to some form of MaskedTextBox to force the user to always enter the date in the same format, and store the date properly as a true date in the database.一种可能性是从TextBox切换到某种形式的MaskedTextBox以强制用户始终以相同格式输入日期,并将日期正确存储为数据库中的真实日期。

After you have a proper DateTime value, you could use simple subtraction to get the person's age, but the TimeSpan value you get back isn't completely useful, as I assume that you need to output the person's age in full years.获得正确的DateTime值后,您可以使用简单的减法来获得此人的年龄,但您返回的TimeSpan值并不完全有用,因为我假设您需要输出该人的整年年龄。

// Assuming dateOfBirth is a DateTime value
TimeSpan age = DateTime.Today - dateOfBirth;

The age variable here holds the amount of time that has passed from the date of birth until today.这里的age变量保存了从出生日期到今天所经过的时间。 Given the fact that years differ in length, this is not the best option.鉴于年份的长度不同,这不是最佳选择。 I would do something like this:我会做这样的事情:

DateTime today = DateTime.Today;
int years = today.Year - dateOfBirth.Year;
if (dateOfBirth.Month > today.Month) {
    --years;
}
else if (dateOfBirth.Month == today.Month && dateOfBirth.Day > today.Day) {
    --years;
}
// TODO: Output the years variable.

If the date is always in the same format, you can use DateTime.TryParseExact() - however this doesn't seem to be the case here.如果日期始终采用相同的格式,您可以使用DateTime.TryParseExact() - 但是这里似乎并非如此。

Perhaps this library helps for you, the author had the same problem: http://www.codeproject.com/Articles/33298/C-Date-Time-Parser也许这个库对你有帮助,作者有同样的问题: http : //www.codeproject.com/Articles/33298/C-Date-Time-Parser

This is no way to guarantee though that every format can be parsed.尽管可以解析每种格式,但这并不能保证。

您可以将DOB string into DateTime object ,然后可以计算年龄:)

DateTime dateDOB = DateTime.Parse(txt_dob.Text); DateTime dateDOB = DateTime.Parse(txt_dob.Text);

int now = int.Parse(DateTime.Today.ToString("yyyyMMdd")); int now = int.Parse(DateTime.Today.ToString("yyyyMMdd"));

int dob = int.Parse(dateDOB.ToString("yyyyMMdd")); int dob = int.Parse(dateDOB.ToString("yyyyMMdd"));

string dif = (now - dob).ToString(); string dif = (now - dob).ToString();

string age = "0";字符串年龄=“0”;

if (dif.Length > 4)如果(差异长度 > 4)

age = dif.Substring(0, dif.Length - 4);年龄 = dif.Substring(0, dif.Length - 4);

txt_age.Text = age.ToString(); txt_age.Text = age.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM