1

我需要帮助来显示这样的东西。如果您认识,这是我们 Stack Overflow 帐户的会员期限。

member for  2 years, 3 months

我需要稍微修改一下。我的条件和显示格式如下:

  Conditions              Display Format
1. Below 7 days  ->       Days (5 days)
2. 7 -30 days    ->       Weeks, days   ( 2 Weeks, 3 Days) in case of 17 days.
3. 30 - 365      ->       Months, Weeks, Days (4Months, 2Weeks, 2Days) in case of 136 days
3. 365 or grter  ->       Years, Months only (2years, 3 Months)

What I did:

我正在使用 Timespan 来获取日期差异。但我对我的输出不满意。我有一个会员,其注册在 2-3 天后将 2 个月大,但仍然显示 1 个月前。这是我的代码:

 TimeSpan ts = DateTime.Now - Convert.ToDateTime(company.RegistrationDate);

                 if (ts.Days > 365)
                    membersince.InnerText = string.Format("{0} years", (ts.Days / 365));
                else if (ts.Days < 30)
                    membersince.InnerText = string.Format("{0} days", ts.Days);
                     else if(ts.Days > 30)
                    membersince.InnerText = string.Format("{0} months", (ts.Days/30));
4

4 回答 4

2

您可以使用Time Period Library for .NET库的DateDiff类:

// ----------------------------------------------------------------------
public void DateDiffSample()
{
  DateTime date1 = new DateTime( 2009, 11, 8, 7, 13, 59 );
  Console.WriteLine( "Date1: {0}", date1 );
  // > Date1: 08.11.2009 07:13:59
  DateTime date2 = new DateTime( 2011, 3, 20, 19, 55, 28 );
  Console.WriteLine( "Date2: {0}", date2 );
  // > Date2: 20.03.2011 19:55:28

  DateDiff dateDiff = new DateDiff( date1, date2 );

  // elapsed
  Console.WriteLine( "DateDiff.ElapsedYears: {0}", dateDiff.ElapsedYears );
  // > DateDiff.ElapsedYears: 1
  Console.WriteLine( "DateDiff.ElapsedMonths: {0}", dateDiff.ElapsedMonths );
  // > DateDiff.ElapsedMonths: 4
  Console.WriteLine( "DateDiff.ElapsedDays: {0}", dateDiff.ElapsedDays );
  // > DateDiff.ElapsedDays: 12
  Console.WriteLine( "DateDiff.ElapsedHours: {0}", dateDiff.ElapsedHours );
  // > DateDiff.ElapsedHours: 12
  Console.WriteLine( "DateDiff.ElapsedMinutes: {0}", dateDiff.ElapsedMinutes );
  // > DateDiff.ElapsedMinutes: 41
  Console.WriteLine( "DateDiff.ElapsedSeconds: {0}", dateDiff.ElapsedSeconds );
  // > DateDiff.ElapsedSeconds: 29
} // DateDiffSample
于 2012-04-08T16:32:57.640 回答
1

为什么这是个问题?如果一个人的年龄是 57 天,那么57 / 30 == 1. 如果您希望2摆脱这种情况,那么您应该

1)确保您的除法结果是双倍的(只需除以30.0而不是30)。原因是这int / int也是一个int.

简单测试:
(57 / 30).GetType().Name == Int32
(57 / 30.0).GetType().Name == Double

(int)1.9 == 1

2)Math.Round()结果。这将是实际的数学舍入,而不是仅删除小数点后的所有内容。

于 2012-04-08T14:25:07.103 回答
1

只需使用TotalDayswhich 会给你加倍(并且更“政治上”正确,Days就像组件一样,尽管在你的情况下是相似的),并且Math.Round()正如@Ilia 已经建议的那样“最近”。

Math.Round(ts.TotalDays);

...或者

String.Format("{0:0.##}", ts.TotalDays);  

...这应该给你喜欢1.7 months(我记得我的头脑或测试你的获胜格式)

这更像是一个逻辑和用户界面问题,如何“呈现”给用户。

编辑:仅TotalDays用于格式化为1.7- 或使用 Math.Round 获得一个月

于 2012-04-08T14:38:31.467 回答
0
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 weeks = days / 7;
  days = days - weeks * 7;
  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 string.Format("{0} Years, {1} Months", years, months);
    else
      return string.Format("{0} Years", years);
  }
  if (months > 0)
  {
    var builder = new StringBuilder();
    builder.AppendFormat("{0} Months", months);
    if (weeks > 0)
      builder.AppendFormat(", {0} Weeks", weeks);
    if (days > 0)
      builder.AppendFormat(", {0} Days", days);
    return builder.ToString();
  }
  if (weeks > 0)
  {
    if (days > 0)
      return string.Format("{0} Weeks, {1} Days", weeks, days);
    return string.Format("{0} Weeks", weeks);
  }

  return string.Format("{0} Days", days);
}
于 2012-04-08T14:52:50.640 回答