Je ne suis pas au courant d'un, mais il semblait comme un problème cool, donc voici mon grand coup à elle (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
Il est vraiment pas aussi sophistiqué que ceux qui existent déjà, mais ça fonctionne, je suppose. Pourrait être une bonne méthode d'extension.
Voir la réponse à propos de NChronic http://stackoverflow.com/questions/14583285/clever-way-to-parse-dates-c-sharp/25588407#25588407 –