Lon*_*l27 1 c# datetime datediff
我的应用程序需要将客户当前年龄调整+0.5(如果已经过去6个月).
代码应该看起来像这样,但是6个月内会有多少个滴答?
if (DateTime.Today - dateOfBirth.Date > new TimeSpan(6))
{
adjust = 0.5M;
}
else
{
adjust = 0M;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
编辑:你知道吗,其实?因为你真正需要的只是在6个月内显示用户的年龄,这就是你应该做的.
static decimal GetApproximateAge(DateTime dateOfBirth) {
TimeSpan age = DateTime.Now - dateOfBirth;
/* a note on the line below:
* treating 182.5 days as equivalent to 6 months,
* reasoning that this will provide acceptable accuracy
* for ages below ~360 years
*
* (result will be 1 day off for roughly every 4 years;
* for a calculation of half-years to be inaccurate
* would take 4 [years] * ~90 [days] = ~360)
*/
// get age in units of 6 months
// desired behavior is not to add 0.5 until
// a full six months have elapsed since the user's last birthday --
// using Math.Floor to ensure this
double approxAgeInHalfYears = Math.Floor(age.TotalDays / 182.5);
// now convert this to years
// did it this way to restrict age to increments of 0.5
double approxAgeInYears = approxAgeInHalfYears * 0.5;
return Convert.ToDecimal(approxAgeInYears);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码包括一个大的评论,解释了自己的缺点,大卫帮助指出.
现在,这是另一种选择.它有点难看,因为它使用循环; 但它也更加坚如磐石,因为它使用了DateTime.AddMonths方法,该方法的优点是已经过Microsoft的测试和记录.
static decimal GetApproximateAge(DateTime dateOfBirth) {
DateTime now = DateTime.Now;
int birthYear = dateOfBirth.Year;
int lastYear = now.Year - 1;
// obviously, if the user's alive, he/she had a birthday last year;
// therefore he/she is at least this old
int minimumAgeInYears = lastYear - birthYear;
// now the question is: how much time has passed since then?
double actualAgeInYears = (double)minimumAgeInYears;
// for every six months that have elapsed since the user's birthday
// LAST year, add 0.5 to his/her age
DateTime birthDateLastYear = new DateTime(lastYear, 1, 1)
.AddDays(dateOfBirth.DayOfYear);
DateTime comparisonDate = birthDateLastYear
.AddMonths(6);
while (comparisonDate < now) {
actualAgeInYears += 0.5;
comparisonDate = comparisonDate.AddMonths(6);
}
return Convert.ToDecimal(actualAgeInYears);
}
Run Code Online (Sandbox Code Playgroud)
人们一直建议检查这个想法dateOfBirth.AddMonths(6)是错误的.由于dateOfBirth是DateTime,它代表用户的出生日期,而不是出生日期.
你要检查的是自用户上一个生日以来已过去六个月- 而不是他们出生的日期.这是一种方法:
DateTime lastBirthDay = GetLastBirthday(dateOfBirth);
if (DateTime.Today > lastBirthDay.AddMonths(6))
{
adjust = 0.5M;
}
else
{
adjust = 0M;
}
DateTime GetLastBirthday(DateTime dateOfBirth)
{
int currentYear = DateTime.Now.Year;
int birthMonth = dateOfBirth.Month;
int birthDay = dateOfBirth.Day;
// if user was born on Feb 29 and this year is NOT a leap year,
// we'll say the user's birthday this year falls on Feb 28
if (birthMonth == 2 && birthDay == 29 && !IsLeapYear(currentYear))
birthDay = 28;
DateTime birthdayThisYear = new DateTime(
currentYear,
birthMonth,
birthDay
);
if (DateTime.Today > birthdayThisYear)
return birthdayThisYear;
else
return birthdayThisYear.AddYears(-1);
}
bool IsLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3152 次 |
| 最近记录: |