65.9K
CodeProject 正在变化。 阅读更多。
Home

用户友好的时间段

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2011 年 3 月 28 日

CPOL
viewsIcon

11916

以日历周期显示 TimeSpan

.NET TimeSpan 的最大值是天数。本示例使用 .NET 时间段库[^] 将 TimeSpan 表示为月和年。一个重要的方面是 TimeSpan 的参考日期。由于月份包含不同数量的天数,相同的持续时间会根据参考日期产生不同的目标日期。下面的代码片段将 TimeSpan 重定向为两个 DateTime,并使用 DateDiff.GetDescription 返回用户友好的持续时间。
// ----------------------------------------------------------------------
public string GetTimeSpanDescription( TimeSpan timeSpan, int precision = int.MaxValue )
{
  return GetTimeSpanDescription( DateTime.Now, timeSpan, precision );
} // GetTimeSpanDescription

// ----------------------------------------------------------------------
public string GetTimeSpanDescription( DateTime moment, TimeSpan timeSpan, int precision = int.MaxValue )
{
  if ( timeSpan == TimeSpan.Zero )
  {
    return string.Empty;
  }

  bool isPositive = timeSpan > TimeSpan.Zero;
  DateTime date1 = isPositive ? moment : moment.Subtract( timeSpan );
  DateTime date2 = isPositive ?  moment.Add( timeSpan ) : moment;

  return new DateDiff( date1, date2 ).GetDescription( precision );
} // GetTimeSpanDescription
现在是一些示例
// ----------------------------------------------------------------------
public void UserFriendlyTimeSpanSample()
{
  TimeSpan lastVisit = new TimeSpan( 400, 7, 25, 0 );
  Console.WriteLine( "last visit before {0}", 
    GetTimeSpanDescription( lastVisit, 3 ) );
    // > last visit before: 1 Year 1 Month 3 Days

  // 60 days
  TimeSpan reminderDuration = new TimeSpan( 60, 0, 0, 0, 0 );
  Console.WriteLine( "reminder duration: {0} Days", reminderDuration.TotalDays );

  DateTime now = DateTime.Now;
  DateTime moment1 = new DateTime( 2011, 4, 1 );
  DateTime moment2 = new DateTime( 2011, 2, 1 );
  DateTime moment3 = new DateTime( 2011, 3, 1 );

  // past
  Console.WriteLine( "last reminder (from now): {0}", 
    GetTimeSpanDescription( now, reminderDuration.Negate() ) );
    // > last reminder (from now): -1 Months -30 Days
  Console.WriteLine( "last reminder ({0:d}): {1}", moment1, 
    GetTimeSpanDescription( moment1, reminderDuration.Negate() ) );
    // > last reminder (01.04.2011): -1 Months -29 Days
  Console.WriteLine( "last reminder ({0:d}): {1}", moment2, 
    GetTimeSpanDescription( moment2, reminderDuration.Negate() ) );
    // > last reminder (01.02.2011): -2 Months -1 Days
  Console.WriteLine( "last reminder ({0:d}): {1}", moment3, 
    GetTimeSpanDescription( moment3, reminderDuration.Negate() ) );
    // > last reminder (01.03.2011): -1 Months -29 Days

  // future
  Console.WriteLine( "next reminder (from now): {0}", 
    GetTimeSpanDescription( now, reminderDuration ) );
    // > next reminder (from now): 1 Month 29 Days
  Console.WriteLine( "next reminder {0:d}: {1}", moment1, 
    GetTimeSpanDescription( moment1, reminderDuration ) );
    // > next reminder 01.04.2011: 1 Month 30 Days
  Console.WriteLine( "next reminder {0:d}: {1}", moment2, 
    GetTimeSpanDescription( moment2, reminderDuration ) );
    // > next reminder 01.02.2011: 2 Months 1 Day
  Console.WriteLine( "next reminder {0:d}: {1}", moment3, 
    GetTimeSpanDescription( moment3, reminderDuration ) );
    // > next reminder 01.03.2011: 1 Month 29 Days
} // UserFriendlyTimeSpanSample
© . All rights reserved.