使用Timespan显示总持续时间

时间:2012-04-08 14:16:27

标签: c# asp.net .net datetime timespan

我需要帮助才能显示这样的内容。如果您认识到,这是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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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()结果。这将是实际的数学舍入,而不是仅仅丢弃小数点后的所有内容。

答案 2 :(得分:1)

只需使用TotalDays即可获得双倍(并且更具'政治性'正确,因为Days只是组件,但在您的情况下类似),Math.Round()为@Ilia已经建议“最近”。

Math.Round(ts.TotalDays);

...或

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

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

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

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

答案 3 :(得分: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);
}