2012-03-01 103 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 
相關問題