基于 C# 中的出生日期的简单年龄计算器



我创建了一个简单的年龄计算器。我想删除小数位,但问题是当我减去nowbday.

:我输入20121023,现在的日期是2014-10-22,因此,当2014.1022 - 2012.1023结果会1.9999...时,我想删除所有小数位并保持整数1,但是我使用的时间String.Format("{0:00}" 它使结果四舍五入为02即使我使用 ConvertToInt32 ,我也不想使用拆分字符串它需要大量代码。

有什么想法吗?

 static void Main(string[] args)
        {
            string year, month, day = string.Empty;
            Console.WriteLine("Enter your Birthdate:");
            Console.WriteLine("Year :");
            year = Console.ReadLine();
            Console.WriteLine("Month :");
             month = Console.ReadLine();
            Console.WriteLine("Day :" );
            day = Console.ReadLine();
            try
            {
                DateTime date = Convert.ToDateTime(year + "-" + month + "-" + day);
                 var bday = float.Parse(date.ToString("yyyy.MMdd"));
                 var now = float.Parse(DateTime.Now.ToString("yyyy.MMdd"));
                 if (now < bday)
                 {
                     Console.WriteLine("Invalid Input of date");
                     Console.ReadLine();
                 }
                 Console.WriteLine("Your Age is " + (String.Format("{0:00}", (now - bday)))); //it rounds off my float
                 Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
        }

}

与评论相反,TimeSpan在这里对您没有帮助,因为一年不是固定的时间长度。这反过来又导致你表达的目标确实很奇怪。您真的不应该将日期表示为小数,前两位数字是月份,第三位和第四位数字是天。时间就是不是这样运作的。(例如,考虑到

2014.0131 和 2014.0201 之间的差异远大于 2014.0130 和 2014.0131 之间的差异。

最好用年、月和日来表示年龄。我的野田时间库使这变得非常简单:

LocalDate birthday = new LocalDate(1976, 6, 19); // For example
LocalDate today = LocalDateTime.FromDateTime(DateTime.Now).Date; // See below
Period period = Period.Between(birthday, today);
Console.WriteLine("You are {0} years, {1} months, {2} days old",
                  period.Years, period.Months, period.Days);

如果你只想确定年份数,你可以决定只使用period.Years,或者也可能根据period.Months对结果进行四舍五入。

但是,我建议不要在生产代码中使用DateTime.Now。在 Noda Time 中,我们有一个IClock接口,表示"获取当前时刻的方法",在主程序集中具有SystemClock实现,在测试程序集中具有FakeClock实现。您的代码将接受一个IClock(可能使用依赖项注入(,然后使用它来确定您感兴趣的任何时区的当前日期。这样,你可以针对你喜欢的任何情况编写测试,而无需更改计算机的时钟。IMO 是处理与时间相关的任务的好方法。

在我们的框架中,我们使用以下方法:

    /// <summary>
    /// Calculates the age at the specified date.
    /// </summary>
    /// <param name="dateOfBirth">The date of birth.</param>
    /// <param name="referenceDate">The date for which the age should be calculated.</param>
    /// <returns></returns>
    public static int Age(DateTime dateOfBirth, DateTime referenceDate)
    {
        int years = referenceDate.Year - dateOfBirth.Year;
        dateOfBirth = dateOfBirth.AddYears(years);
        if (dateOfBirth.Date > referenceDate.Date)
            years--;
        return years;
    }
    /// <summary>
    /// Calculates the age at this moment.
    /// </summary>
    /// <param name="dateOfBirth">The date of birth.</param>
    /// <returns></returns>
    public static int Age(DateTime dateOfBirth)
    {
        return Age(dateOfBirth, DateTime.Today);
    }

在这里我找到了问题的答案,我使用 LastIndexOf 删除某个字符 point(.( 之后的所有字符串,但在此之前我float转换为 string .

这是准确的,试试看。 :)

   static void Main(string[] args)
    {
        string year, month, day = string.Empty;
        Console.WriteLine("Enter your Birthdate:");
        Console.WriteLine("Year :");
        year = Console.ReadLine();
        Console.WriteLine("Month :");
         month = Console.ReadLine();
        Console.WriteLine("Day :" );
        day = Console.ReadLine();
        try
        {
            DateTime date = Convert.ToDateTime(year + "-" + month + "-" + day);
             var bday = float.Parse(date.ToString("yyyy.MMdd"));
             var now = float.Parse(DateTime.Now.ToString("yyyy.MMdd"));
             if (now < bday)
             {
                 Console.WriteLine("Invalid Input of date");
                 Console.ReadLine();
             }
             string age = (now - bday).ToString(); 
             Console.WriteLine("Your Age is " + (age.Substring(0, age.LastIndexOf('.'))));
             Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadLine();
        }


        }

最新更新