给定代表某人生日的DateTime,我如何计算他们的年龄(以年为单位)?
当前回答
我认为这个问题可以用这样一种更简单的方法解决-
该类可以是-
using System;
namespace TSA
{
class BirthDay
{
double ageDay;
public BirthDay(int day, int month, int year)
{
DateTime birthDate = new DateTime(year, month, day);
ageDay = (birthDate - DateTime.Now).TotalDays; //DateTime.UtcNow
}
internal int GetAgeYear()
{
return (int)Math.Truncate(ageDay / 365);
}
internal int GetAgeMonth()
{
return (int)Math.Truncate((ageDay % 365) / 30);
}
}
}
电话可以是这样的-
BirthDay b = new BirthDay(1,12,1990);
int year = b.GetAgeYear();
int month = b.GetAgeMonth();
其他回答
这里有一个非常简单且易于遵循的示例。
private int CalculateAge()
{
//get birthdate
DateTime dtBirth = Convert.ToDateTime(BirthDatePicker.Value);
int byear = dtBirth.Year;
int bmonth = dtBirth.Month;
int bday = dtBirth.Day;
DateTime dtToday = DateTime.Now;
int tYear = dtToday.Year;
int tmonth = dtToday.Month;
int tday = dtToday.Day;
int age = tYear - byear;
if (bmonth < tmonth)
age--;
else if (bmonth == tmonth && bday>tday)
{
age--;
}
return age;
}
我对Mark Soen的答案做了一个小小的修改:我重写了第三行,以便可以更容易地解析表达式。
public int AgeInYears(DateTime bday)
{
DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday.AddYears(age) > now)
age--;
return age;
}
为了清晰起见,我还将其转换为函数。
这里有一个DateTime扩展程序,它将年龄计算添加到DateTime对象。
public static class AgeExtender
{
public static int GetAge(this DateTime dt)
{
int d = int.Parse(dt.ToString("yyyyMMdd"));
int t = int.Parse(DateTime.Today.ToString("yyyyMMdd"));
return (t-d)/10000;
}
}
我认为这个问题可以用这样一种更简单的方法解决-
该类可以是-
using System;
namespace TSA
{
class BirthDay
{
double ageDay;
public BirthDay(int day, int month, int year)
{
DateTime birthDate = new DateTime(year, month, day);
ageDay = (birthDate - DateTime.Now).TotalDays; //DateTime.UtcNow
}
internal int GetAgeYear()
{
return (int)Math.Truncate(ageDay / 365);
}
internal int GetAgeMonth()
{
return (int)Math.Truncate((ageDay % 365) / 30);
}
}
}
电话可以是这样的-
BirthDay b = new BirthDay(1,12,1990);
int year = b.GetAgeYear();
int month = b.GetAgeMonth();
我使用ScArcher2的解决方案来精确计算人的年龄,但我需要进一步计算他们的月和日以及年。
public static Dictionary<string,int> CurrentAgeInYearsMonthsDays(DateTime? ndtBirthDate, DateTime? ndtReferralDate)
{
//----------------------------------------------------------------------
// Can't determine age if we don't have a dates.
//----------------------------------------------------------------------
if (ndtBirthDate == null) return null;
if (ndtReferralDate == null) return null;
DateTime dtBirthDate = Convert.ToDateTime(ndtBirthDate);
DateTime dtReferralDate = Convert.ToDateTime(ndtReferralDate);
//----------------------------------------------------------------------
// Create our Variables
//----------------------------------------------------------------------
Dictionary<string, int> dYMD = new Dictionary<string,int>();
int iNowDate, iBirthDate, iYears, iMonths, iDays;
string sDif = "";
//----------------------------------------------------------------------
// Store off current date/time and DOB into local variables
//----------------------------------------------------------------------
iNowDate = int.Parse(dtReferralDate.ToString("yyyyMMdd"));
iBirthDate = int.Parse(dtBirthDate.ToString("yyyyMMdd"));
//----------------------------------------------------------------------
// Calculate Years
//----------------------------------------------------------------------
sDif = (iNowDate - iBirthDate).ToString();
iYears = int.Parse(sDif.Substring(0, sDif.Length - 4));
//----------------------------------------------------------------------
// Store Years in Return Value
//----------------------------------------------------------------------
dYMD.Add("Years", iYears);
//----------------------------------------------------------------------
// Calculate Months
//----------------------------------------------------------------------
if (dtBirthDate.Month > dtReferralDate.Month)
iMonths = 12 - dtBirthDate.Month + dtReferralDate.Month - 1;
else
iMonths = dtBirthDate.Month - dtReferralDate.Month;
//----------------------------------------------------------------------
// Store Months in Return Value
//----------------------------------------------------------------------
dYMD.Add("Months", iMonths);
//----------------------------------------------------------------------
// Calculate Remaining Days
//----------------------------------------------------------------------
if (dtBirthDate.Day > dtReferralDate.Day)
//Logic: Figure out the days in month previous to the current month, or the admitted month.
// Subtract the birthday from the total days which will give us how many days the person has lived since their birthdate day the previous month.
// then take the referral date and simply add the number of days the person has lived this month.
//If referral date is january, we need to go back to the following year's December to get the days in that month.
if (dtReferralDate.Month == 1)
iDays = DateTime.DaysInMonth(dtReferralDate.Year - 1, 12) - dtBirthDate.Day + dtReferralDate.Day;
else
iDays = DateTime.DaysInMonth(dtReferralDate.Year, dtReferralDate.Month - 1) - dtBirthDate.Day + dtReferralDate.Day;
else
iDays = dtReferralDate.Day - dtBirthDate.Day;
//----------------------------------------------------------------------
// Store Days in Return Value
//----------------------------------------------------------------------
dYMD.Add("Days", iDays);
return dYMD;
}