1

私のアプリケーションでは、最後の誕生日から 6 か月が経過している場合、クライアントの現在の年齢を +0.5 だけ調整する必要があります。

コードは次のようになりますが、6 か月でティックはいくつになるでしょうか?

if (DateTime.Today - dateOfBirth.Date > new TimeSpan(6))
        {
            adjust = 0.5M;
        }
        else
        {
            adjust = 0M;
        }

前もって感謝します

4

8 に答える 8

6

編集:あなたは実際に何を知っていますか?明らかに本当に必要なのは、ユーザーの年齢を 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);
}

上記のコードには、それ自体の欠点を説明する大きなコメントが含まれており、David が指摘してくれて助かりました。

さて、ここにさらに別のオプションがあります。ループを使用しているため、ちょっと醜いです。しかし、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);
}

人々がチェックすることを提案しているというこの考えdateOfBirth.AddMonths(6)間違っています。dateOfBirthDateTimeであるため、ユーザーの生年月日はなく生年月日を表します

確認したいのは、ユーザーの生年月日ではなく、ユーザーの最後の誕生日から 6 か月が経過したかどうかです。これを行う1つの方法は次のとおりです。

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);
}
于 2010-04-29T15:16:54.610 に答える
3

if (dateOfBirth.Date.AddMonths(6) < DateTime.Today)代わりに、なぜですか?

于 2010-04-29T15:16:55.270 に答える
1
        long ticks = new DateTime(0).AddMonths(6).Ticks;
        TimeSpan ts = new TimeSpan(ticks);
于 2010-04-29T15:17:01.353 に答える
1
if (dateOfBirth.Date.AddMonths(6) < DateTime.Today)
{
   age += 0.5;
}
于 2010-04-29T15:20:57.113 に答える
0
DateTime today = DateTime.Today;
DateTime lastBirthday = dateOfBirth.Date.AddYears(today.Year - dateOfBirth.Year);
if (lastBirthday > today) lastBirthday = lastBirthday.AddYears(-1);

if (today > lastBirthday.AddMonths(6))
    adjust = 0.5M;
else
    adjust = 0M;
于 2010-04-29T17:03:04.060 に答える
0

物事を過度に複雑にしている可能性があると思います:

DateTime displayDate = User.BirthDate; // User.BirthDate is a mock for however you get the birth date

if(DateTime.Now.AddMonths(-6) > displayDate)
{
    // More than 6 Months have passed, so perform your logic to add .5 years
}
于 2010-04-29T15:17:41.163 に答える
0

このようなもの?

if (DateTime.Now.AddMonths(-6) > dateofBirth.Date)
{
    dateOfBirth = dateOfBirth.AddMonths(6);
}
于 2010-04-29T15:17:47.967 に答える
0

前回の誕生日から 6 か月ではなく半年かどうかを実際に知りたいと思います (月の長さはさまざまなので)。

    int daysDiff = DateTime.Now.DayOfYear - dayofBirth.DayOfYear;
    if (daysDiff <0) daysDiff += 365;
    double adjust = daysDiff > 365/2 ? 0.5 : 0.0;
于 2010-04-29T15:45:12.310 に答える