2012-03-01 101 views
0

我不知道很exatly在我的例子做什么IM,但我需要我的函数返回是这样表示日期时间系列的年月周分钟前

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

回答

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(); 
+0

感谢您的回复我会给那个以前,你有没有测试过它?唯一的部分是硬编码的30个月,其中一些有31我唯一担心,如果你知道如何解决你的代码更新 – ONYX 2012-03-01 01:42:39

+0

在处理日历时,是“月”可以从28天到31天不等。在处理时间间隔时,“月”是您定义的任何内容,因为它只是简写。 – BACON 2012-03-01 02:15:58

+0

嗯,以p.Years开头,它不承认年,所以我会把p.TotalDays/365 – ONYX 2012-03-01 23:22:05

0

可以使用则DateDiffTime Period Library for .NET

// ---------------------------------------------------------------------- 
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 
相关问题