C# 如何根据生日计算年龄?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2194999/
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
How to calculate an age based on a birthday?
提问by nacho10f
Possible Duplicate:
How do I calculate someone’s age in C#?
可能的重复:
如何在 C# 中计算某人的年龄?
I want to write an ASP.NET helper method which returns the age of a person given his or her birthday.
我想编写一个 ASP.NET 帮助方法,该方法返回给定生日的人的年龄。
I've tried code like this:
我试过这样的代码:
public static string Age(this HtmlHelper helper, DateTime birthday)
{
return (DateTime.Now - birthday); //??
}
But it's not working. What is the correct way to calculate the person's age based on their birthday?
但它不起作用。根据生日计算人的年龄的正确方法是什么?
采纳答案by Pierre-Alain Vigeant
Stackoverflow uses such function to determine the age of a user.
Stackoverflow 使用这样的函数来确定用户的年龄。
The given answer is
给出的答案是
DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (now < bday.AddYears(age)) age--;
So your helper method would look like
所以你的辅助方法看起来像
public static string Age(this HtmlHelper helper, DateTime birthday)
{
DateTime now = DateTime.Today;
int age = now.Year - birthday.Year;
if (now < birthday.AddYears(age)) age--;
return age.ToString();
}
Today, I use a different version of this function to include a date of reference. This allow me to get the age of someone at a future date or in the past. This is used for our reservation system, where the age in the future is needed.
今天,我使用此函数的不同版本来包含参考日期。这使我能够在未来某个日期或过去获得某人的年龄。这用于我们的预订系统,其中需要未来的年龄。
public static int GetAge(DateTime reference, DateTime birthday)
{
int age = reference.Year - birthday.Year;
if (reference < birthday.AddYears(age)) age--;
return age;
}
回答by Jake Rios
I don't really understand why you would make this an HTML Helper. I would make it part of the ViewData dictionary in an action method of the controller. Something like this:
我真的不明白你为什么要把它变成一个 HTML 助手。我会将它作为控制器操作方法中的 ViewData 字典的一部分。像这样的东西:
ViewData["Age"] = DateTime.Now.Year - birthday.Year;
Given that birthday is passed into an action method and is a DateTime object.
鉴于生日被传递到一个动作方法并且是一个 DateTime 对象。
回答by Frederik Gheysels
I do it like this:
我这样做:
(Shortened the code a bit)
(稍微缩短了代码)
public struct Age
{
public readonly int Years;
public readonly int Months;
public readonly int Days;
}
public Age( int y, int m, int d ) : this()
{
Years = y;
Months = m;
Days = d;
}
public static Age CalculateAge ( DateTime birthDate, DateTime anotherDate )
{
if( startDate.Date > endDate.Date )
{
throw new ArgumentException ("startDate cannot be higher then endDate", "startDate");
}
int years = endDate.Year - startDate.Year;
int months = 0;
int days = 0;
// Check if the last year, was a full year.
if( endDate < startDate.AddYears (years) && years != 0 )
{
years--;
}
// Calculate the number of months.
startDate = startDate.AddYears (years);
if( startDate.Year == endDate.Year )
{
months = endDate.Month - startDate.Month;
}
else
{
months = ( 12 - startDate.Month ) + endDate.Month;
}
// Check if last month was a complete month.
if( endDate < startDate.AddMonths (months) && months != 0 )
{
months--;
}
// Calculate the number of days.
startDate = startDate.AddMonths (months);
days = ( endDate - startDate ).Days;
return new Age (years, months, days);
}
// Implement Equals, GetHashCode, etc... as well
// Overload equality and other operators, etc...
}
}
回答by Rubens Farias
Another clever way from that ancient thread:
来自那个古老线程的另一个聪明的方法:
int age = (
Int32.Parse(DateTime.Today.ToString("yyyyMMdd")) -
Int32.Parse(birthday.ToString("yyyyMMdd"))) / 10000;