2014-10-22 287 views
3

我创建了一个简单的年龄计算器。我想删除小数位数,但问题是当我将now减去bday时。简单年龄计算器根据出生日期在C#

例如:我输入20121023和日期现在是2014-10-22, 所以当2014.1022 - 2012.1023的结果将是1.9999...我想删除所有小数位,并保持整体数量1,但我在使用的时候String.Format("{0:00}"即使当我使用ConvertToInt32时,它将结果舍入为02,我不想使用split 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(); 

       } 
       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(); 
      } 

     } 

}

+1

为什么你不使用TimeSpan类? – 2014-10-22 09:17:32

+0

1.您可以使用'TimeSpan'。 2.看看这个答案http:// stackoverflow。com/questions/16671165 /如何计算一个人的年龄,以了解如何自己计算日期 – AsfK 2014-10-22 09:18:22

+1

@ JennyO'Reilly:你无法从TimeSpan获得数年... – 2014-10-22 09:19:47

回答

-1

Here我发现一个回答我的问题,我用LastIndexOf某个字符是点后删除所有的字符串,但在此之前我将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(); 
     } 





     } 
+0

我投降,因为有几个解决方案发布,仍然用完全过于复杂的方法回答自己的问题。 – 2014-10-23 06:22:17

+0

@ JennyO'Reilly,你在谈论世界上的什么。尽管如此,我仍然没有得到任何解决方案。我试图解决它我自己 – Awtszs 2014-10-23 06:25:55

+0

你张贴自己的解决方案实际上已经建议我(使用Math.Floor)和乔恩Skeet(铸造为一个整数)使用一种更简单的方法。所以你的解决方案对我来说毫无意义。您的解决方案以何种方式不同? – 2014-10-23 09:04:18

9

相反的意见,TimeSpan帮助你在这里,因为一年是不是一个固定的时间长度。这反过来导致你表达的目标确实很奇怪。您实际上不应该将日期表示为小数,前两位数字为月份,第三位和第四位数字为天数。时间就是这样。 (例如,2014.0131与2014.0201之间的差异比2014.0130与2014.0131之间的差异大得多)。

最好用年,月,日来表示年龄。我Noda Time库使得这很简单:

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中,我们有一个代表“获取当前时间即时”的接口,SystemClock实现在主要程序集中,FakeClock实现在测试程序集中。您的代码将接受IClock(可能使用依赖注入),然后使用该代码确定您感兴趣的任何时区的当前日期。这样,您可以针对任何喜欢的情况编写测试,而无需更改计算机的时钟。一般来说,这是处理时间相关任务的一种好方法,IMO。

2

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

/// <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); 
    }