C# 准确年龄计算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:01:03  来源:igfitidea点击:

Exact age calculation

提问by Nelson Miranda

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

可能的重复:
如何在 C# 中计算某人的年龄?

Maybe this could be silly but and I don't have issues with my age but sometimes it is good to calculate the exact age of someone, I have introduced my birthdate in my profile (01/12/1975) "dd/mm/yyyy" and it calculated 33 and I'm 32 actually still, isn't it better to calculate the exact age?

也许这可能很愚蠢,但我的年龄没有问题,但有时计算某人的确切年龄是很好的,我在我的个人资料中介绍了我的生日(01/12/1975)“dd/mm/yyyy ” 算出来是 33 岁,其实我还是 32 岁,算出准确的年龄不是更好吗?

Maybe

也许

DateTime dt1 = DateTime.Now;
TimeSpan dt2;
dt2 = dt1.Subtract(new DateTime(1975, 12, 01));
double year = dt2.TotalDays / 365;

The result of year is 32.77405678074

年份的结果是 32.77405678074

Could this code be OK?

这段代码可以吗?

采纳答案by Wedge

If you were born on January 12th 1975, you would be 33 years old today.

如果你出生在 1975 年 1 月 12 日,那么今天你就 33 岁了。

If you were born on December 1st 1975, you would be 32 years old today.

如果你出生在 1975 年 12 月 1 日,那么你今天就 32 岁了。

If you read the note by the birthday field when editing your profile you'll see it says "YYYY/MM/DD", I'm sure it will try to interpret dates of other formats but it looks like it interprets MM/DD/YYYY (US standard dates) in preference to DD/MM/YYYY (European standard dates). The easy fix is to enter the date of your birthday according to the suggested input style.

如果您在编辑个人资料时阅读生日字段旁的注释,您会看到它显示“YYYY/MM/DD”,我确定它会尝试解释其他格式的日期,但看起来它会解释 MM/DD/ YYYY(美国标准日期)优先于 DD/MM/YYYY(欧洲标准日期)。简单的解决方法是根据建议的输入样式输入您的生日日期。

回答by Kevin Crumley

Maybe this could be silly but and I don't have issues with my age but sometimes it is good to calculate the exact age of someone, I have introduced my birthdate in my profile (01/12/1975) "dd/mm/yyyy" and it calculated 33 and I'm 32 actually still, doesn't it better to calculate the exact age?

也许这可能很愚蠢,但我的年龄没有问题,但有时计算某人的确切年龄是很好的,我在我的个人资料中介绍了我的生日(01/12/1975)“dd/mm/yyyy “它计算了 33 而我实际上仍然是 32,计算确切年龄不是更好吗?

My guess would be that this is a localization issue, though I don't know how it would happen, since (at least for me) the profile has you fill out your age in the format "YYYY/MM/DD". But your birthday is one that reads as a valid date (January 12th) in traditional U.S. settings, so this is the area I'd look into. I was born in 1975, also, and my birthday is next month, and it's got my age right.

我的猜测是这是一个本地化问题,尽管我不知道它会如何发生,因为(至少对我而言)个人资料让您以“YYYY/MM/DD”格式填写您的年龄。但是在传统的美国环境中,您的生日是一个有效日期(1 月 12 日),所以这就是我要研究的领域。我也是 1975 年出生的,我的生日是下个月,正好适合我的年龄。

回答by Kibbee

Actually, because of leap years, your code would be off. Since the timespan object has no TotalYears property the best way to get it would be this

实际上,由于闰年,您的代码会关闭。由于时间跨度对象没有 TotalYears 属性,因此获得它的最佳方法是这样

Pardon the VB.Net

原谅 VB.Net

Dim myAge AS Integer = DateTime.Now.year - BirthDate.year
If Birthdate.month < DateTime.Now.Month _
OrElse BirthDate.Month = DateTime.Now.Month AndAlso Birthdate.Day < DateTime.Now.Day Then
MyAge -= 1
END IF

回答by Devarajan.T

int ag1;
string st, ag;
void agecal()
{
    st = TextBox4.Text;
    DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
    dtfi.ShortDatePattern = "MM/dd/yyyy";
    dtfi.DateSeparator = "/";
    DateTime dt = Convert.ToDateTime(st, dtfi);
    ag1 = int.Parse(dt.Year.ToString());
    int years = DateTime.Now.Year - ag1;
    ag = years.ToString();
    TextBox3.Text = ag.ToString();
}