C# 确定c中的生日是否已过了6个月#

C# 确定c中的生日是否已过了6个月#,c#,datetime,datediff,C#,Datetime,Datediff,我的应用程序需要将客户的当前年龄调整+0.5,如果距离他们上次生日已经6个月了 代码应该是这样的,但是6个月内会有多少滴答声 if (DateTime.Today - dateOfBirth.Date > new TimeSpan(6)) { adjust = 0.5M; } else { adjust = 0M; } 提前感谢编辑:你知道吗?很明显,你真正需要

我的应用程序需要将客户的当前年龄调整+0.5,如果距离他们上次生日已经6个月了

代码应该是这样的,但是6个月内会有多少滴答声

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

提前感谢编辑:你知道吗?很明显,你真正需要的只是在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);
}

编辑:实际上,你知道吗?很明显,你真正需要的只是在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);
}

为什么不把if(dateOfBirth.Date.AddMonths(6)改成if(dateOfBirth.Date.AddMonths(6)呢?

我想你可能把事情复杂化了:

        long ticks = new DateTime(0).AddMonths(6).Ticks;
        TimeSpan ts = new TimeSpan(ticks);
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
}

我想你可能把事情搞得太复杂了:

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
}
像这样的

if (DateTime.Now.AddMonths(-6) > dateofBirth.Date)
{
    dateOfBirth = dateOfBirth.AddMonths(6);
}
像这样的

if (DateTime.Now.AddMonths(-6) > dateofBirth.Date)
{
    dateOfBirth = dateOfBirth.AddMonths(6);
}
if(dateOfBirth.Date.AddMonths(6)
if(dateOfBirth.Date.AddMonths(6)
我想你真的想知道他们的最后一个生日是半年,而不是六个月(因为月的长度不同)

int daysDiff=DateTime.Now.DayOfYear-dayofBirth.DayOfYear;
如果(日期DIFF 365/2-0.5:0.0;

我想你真的想知道他们的最后一个生日是半年,而不是六个月(因为月的长度不同)

int daysDiff=DateTime.Now.DayOfYear-dayofBirth.DayOfYear;
如果(日期DIFF 365/2-0.5:0.0;


希望您的出生日期没有实际的出生年份…您向客户支付了多少费用?出生日期被标准化为当前年份?这样一个简单的问题引起了如此多的错误回答…这对未来的节目不是好兆头希望您的出生日期没有实际的出生年份…您的收入是多少向您的客户结账?出生日期被标准化为当前年份?如此简单的问题引发了如此多的错误回答…这对未来的编程来说不是好兆头您意识到假设此人至少六个月大,这将始终是真的吗?非常好的一点。我想我正忙于忽略以下事实:sp指定代码会将年龄调整0.5,无论这是否正确(我们不知道测试是在生日后6个月进行的,是6个月还是半年)或者,如果之前已经做过增量。从其他答案来看,这种盲目性似乎并不罕见。也许我应该用更正确的方法编辑答案(当然,这意味着我需要处理2月29日的DoB-我们是希望从上一个2月29日起6个月,还是从2月28日或3月1日起6个月?)对不起,我应该提到的是我感兴趣的上一个生日的人。我在2月29日更新了我的问题good call。我在冗长的回答中添加了一个检查。你知道假设这个人至少六个月大,这将始终是真的吗?很好的一点。我想我正忙于忽略以下事实,即ied代码会将年龄调整0.5,不管这是否正确(我们不知道测试是在生日后6个月进行的,是6个月还是半年)或者,如果之前已经做过增量。从其他答案来看,这种盲目性似乎并不罕见。也许我应该用更正确的方法编辑答案(当然,这意味着我需要处理2月29日的DoB-我们是希望从上一个2月29日起6个月,还是从2月28日或3月1日起6个月?)对不起,我应该提到的是我感兴趣的上一个生日的人。2月29日更新了我的问题Good call。我在冗长的回答中添加了一个复选框。
dateOfBirth.AddMonths(6);
什么也不做。它只返回一个值。@Germ:DateTime是不可变的。您不能更改它,您必须执行
dateOfBirth=dateOfBirth.AddMonths(6)
还是不好。就像David的回答一样,只要用户至少六个月大,这将使出生日期增加六个月。此外,我认为想法是将用户的年龄增加0.5个月,而不是他们的出生日期!
出生日期。添加月数(6);
什么也不做。它只返回一个值。@Germ:DateTime是不可变的。您不能更改它,您必须执行
dateOfBirth=dateOfBirth.AddMonths(6)
这仍然不好。就像David的回答一样,只要用户至少六个月大,这将为
出生日期增加六个月。此外,我认为我们的想法是给用户的年龄增加0.5个月,而不是他们的出生日期!谢谢Dan,你发布了一些非常有用的信息。你已经部分修复了2月29日的错误,但你仍然拥有Lotus 1-2-3 leap year bug:假设任何可被4整除的年份都是闰年,并使用2月29日的日期表示这些年份。可被100整除的年份不是闰年,除非它们也可被400整除。这意味着1900年2月29日将抛出一个参数异常(如果出生日期出现,我必须使用Lotus/Excel)(Excel使用Lotus dates向后兼容)不喜欢生活