生日年龄的计算,也是月数和日数 [英] birthday age calculation, but also for number of months and days

查看:151
本文介绍了生日年龄的计算,也是月数和日数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,所以我已经实现了这个解决方案,以从生日日期输入获得用户的生日:



这样做很棒,但是我需要解释生日的年龄不到一年(婴儿,婴儿)。如果bdate和当前日期之间的时间不到365天,那么现在只给我一个0的年龄。



我在想什么是这样的:

  public string calculateAge(DateTime birthDate,DateTime now)
{
// BDay在不同的年(年龄> 1)
int age = now.Year - birthDate.Year;
if(now.Month< birthDate.Month ||(now.Month == birthDate.Month&& now.Day< birthDate.Day))age--

if(age == 0)
{
// Bday在同一年
age = now.Month - birthDate.Month;
if(now.Month< birthDate.Month ||(now.Month == birthDate.Month&& now.Day< birthDate.Day))age--

return age.ToString()+months;
}
if(age == 0)
{
// Bday在同一个月
age = now.Day - birthDate.Day;
if(now.Month< birthDate.Month ||(now.Month == birthDate.Month&& now.Day< birthDate.Day))age--

return age.ToString()+days;
}
return age.ToString();
}

然而,我的一些测试Bdays给了我这个:

 (今日:3/6/2012)
Bday1 = 3/5/2012
年龄结果= -1
预期结果= 1天$ ​​b
$ b Bday2 = 3/1/2012
年龄结果= 0个月
预期结果= 5天

Bday3 = 1/1/2012
年龄结果= 2个月
预期结果= 2个月(这很好)

Bday4 = 3/7/2011
年龄结果= -1个月
预期结果= 11个月

Bday5 = 3/1/2011
年龄结果= 1
预期结果= 1(这很好)

你可以看到,由于当前的设置,这个问题是由于bday月份小于当前月份可能会产生一些负数。



我也看到无法进入天循环的错误,但我认为这是一个假设。让我知道,如果你有任何见解我能做什么来获得预期的结果。另外如果你需要更多的信息,如测试bdays。谢谢!

解决方案

  static public string calculateAge(DateTime birthDate,DateTime now)
{
birthDate = birthDate.Date;
now = now.Date;

var days = now.Day - birthDate.Day;
if(days< 0)
{
var newNow = now.AddMonths(-1);
days + =(int)(now - newNow).TotalDays;
now = newNow;
}
var months = now.Month - birthDate.Month;
if(months< 0)
{
months + = 12;
now = now.AddYears(-1);
}
var years = now.Year - birthDate.Year;
if(years == 0)
{
if(months == 0)
return days.ToString()+days;
else
return months.ToString()+months;
}
return years.ToString();
}

结果(现在 - 3/7/2012):

  3/5/2012:2天
3/1/2012:6天
1/1/2012: 2个月
3/8/2011:11个月
3/1/2011:1


Hello so I've implemented this solution to attain the user's birthday from a birthday date input: How do I calculate someone's age in C#?

This works great however I do need to interpret birthday's for ages less than a year (babies, infants). It will current just give me an age of "0" if there are less than 365 days between the bdate and current date.

What I'm thinking is something like this:

public string calculateAge(DateTime birthDate, DateTime now)
        {
            //BDay is in different year (age > 1)
            int age = now.Year - birthDate.Year;
            if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--;

            if (age == 0)
            {
                //Bday is in same year
                age = now.Month - birthDate.Month;
                if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--;

                return age.ToString() + " months";
            }
            if (age == 0)
            {
                //Bday is in the same month
                age = now.Day - birthDate.Day;
                if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--;

                return age.ToString() + " days";
            }
            return age.ToString();
        }

However some of my test Bdays give me this:

(Today's date: 3/6/2012)
Bday1 = 3/5/2012
Age result = -1
Expected result = 1 day

Bday2 = 3/1/2012
Age result = 0 months
Expected result = 5 days

Bday3 = 1/1/2012
Age result = 2 months
Expected result = 2 months (this is fine)

Bday4 = 3/7/2011
Age result = -1 months
Expected result = 11 months

Bday5 = 3/1/2011
Age result = 1
Expected result = 1 (this is fine) 

You can see that because of how it is currently setup the issue is stemming from when the bday month is less than the current month some negative numbers can result.

I also see the error about not being able to get to the "days" loop, but I think that's a moot point right now. Let me know if you have any insight on what I can do to get the desired results. Also if you need more info like test bdays too. Thanks!

解决方案

static public string calculateAge(DateTime birthDate, DateTime now)
{
  birthDate = birthDate.Date;
  now = now.Date;

  var days = now.Day - birthDate.Day;
  if (days < 0)
  {
    var newNow = now.AddMonths(-1);
    days += (int)(now - newNow).TotalDays;
    now = newNow;
  }
  var months = now.Month - birthDate.Month;
  if (months < 0)
  {
    months += 12;
    now = now.AddYears(-1);
  }
  var years = now.Year - birthDate.Year;
  if (years == 0)
  {
    if (months == 0)
      return days.ToString() + " days";
    else
      return months.ToString() + " months";
  }
  return years.ToString();
} 

results (for now - 3/7/2012):

  3/5/2012: 2 days
  3/1/2012: 6 days
  1/1/2012: 2 months
  3/8/2011: 11 months
  3/1/2011: 1

这篇关于生日年龄的计算,也是月数和日数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆