nie jestem świadomy jednego, ale brzmiało to chłodnym problemu, więc oto moja walnięcie w nim (VB.NET):
Private Function ConvertDateTimeToStringRelativeToNow(ByVal d As DateTime) As String
Dim diff As TimeSpan = DateTime.Now().Subtract(d)
If diff.Duration.TotalMinutes < 1 Then Return "Now"
Dim str As String
If diff.Duration.TotalDays > 365 Then
str = CInt(diff.Duration.TotalDays/365).ToString() & " years"
ElseIf diff.Duration.TotalDays > 30 Then
str = CInt(diff.TotalDays/30).ToString() & " months"
ElseIf diff.Duration.TotalHours > 24 Then
str = CInt(diff.Duration.TotalHours/24) & " days"
ElseIf diff.Duration.TotalMinutes > 60 Then
str = CInt(diff.Duration.TotalMinutes/60) & " minutes"
Else
str = CInt(diff.Duration.TotalMinutes).ToString() & " minutes"
End If
If str.StartsWith("1") Then str = str.SubString(0, str.Length - 1)
If diff.TotalDays > 0 Then
str &= " ago"
Else
str &= " from now"
End If
Return str
End Function
To naprawdę nie jest tak skomplikowane, jak te, które już istnieją, ale chyba wszystko działa dobrze. To może być dobra metoda rozszerzenia.
Zobacz odpowiedź na temat NChronic http://stackoverflow.com/questions/14583285/clever-way-to-parse-dates-c-sharp/25588407#25588407 –