DateTime系列的几个月和几个星期前

时间:2012-03-01 01:26:34

标签: c# asp.net

我不太清楚地知道我在我的例子中做了什么,但我需要我的函数返回的是一个字符串,表示如下

1yr, 2 months or
1yr or
2months or
2months 2weeks or
3mins ago

如果有人知道怎么回事,那么请留下答案

private string GetTimeSpan(DateTime creationDate)
{
    string timespan = "";
    if (Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25) >= 1)
    {
        timespan += ((int)Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25)).ToString() + "yr, ";
    }
    else if (Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25) < 1)
    {
        timespan += ((int)Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25)).ToString();
    }

    return timespan;
}

2 个答案:

答案 0 :(得分:1)

System.DateTime- operator overloaded,需要两个DateTime s(因为它是二元运算符)并返回TimeSpan个实例:

TimeSpan span = DateTime.Now - someOtherTime;

这应该会得到TimeSpan,表示两个DateTime实例之间的时间。要打印出你想要的字符串,你可以通过扩展方法做这样的事情:

public static string Print(this TimeSpan p) 
{
  var sb = new StringBuilder();
  if(p.Days > 365)
    sb.AppendFormat("{0}yr, ", p.Years / 365);
  if(p.Days % 365 > 30) // hard-code 30 as month interval...
    sb.AppendFormat("{0}months, ", ( p.Days % 365 ) /30);
  if(p.Days % 365 % 30 > 7) 
    sb.AppendFormat("{0}weeks, ", p.Days % 365 % 30 / 7);
  if(p.Days % 365 % 30 % 7 > 0)
    sb.AppendFormat("{0}days, ", p.Days % 365 % 30 % 7);
  if(p.Hours > 0)
    sb.AppendFormat("{0}hr, ", p.Hours);
  // ... and so on ...
  sb.Remove(sb.Length - 2, 2); // remove the last ", " part.
  return sb.ToString();
}

然后你就像使用它一样:

string span = (DateTime.Now - creationDate).Print();

答案 1 :(得分:0)

您可以使用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 );

  Console.WriteLine( "DateDiff.GetDescription(1): {0}", dateDiff.GetDescription( 1 ) );
  // > DateDiff.GetDescription(1): 1 Year
  Console.WriteLine( "DateDiff.GetDescription(2): {0}", dateDiff.GetDescription( 2 ) );
  // > DateDiff.GetDescription(2): 1 Year 4 Months
  Console.WriteLine( "DateDiff.GetDescription(3): {0}", dateDiff.GetDescription( 3 ) );
  // > DateDiff.GetDescription(3): 1 Year 4 Months 12 Days
  Console.WriteLine( "DateDiff.GetDescription(4): {0}", dateDiff.GetDescription( 4 ) );
  // > DateDiff.GetDescription(4): 1 Year 4 Months 12 Days 12 Hours
  Console.WriteLine( "DateDiff.GetDescription(5): {0}", dateDiff.GetDescription( 5 ) );
  // > DateDiff.GetDescription(5): 1 Year 4 Months 12 Days 12 Hours 41 Mins
  Console.WriteLine( "DateDiff.GetDescription(6): {0}", dateDiff.GetDescription( 6 ) );
  // > DateDiff.GetDescription(6): 1 Year 4 Months 12 Days 12 Hours 41 Mins 29 Secs
} // DateDiffSample