Datumsberechnung Diskrepanzanalyse (JavaScript VS .NET)
-
29-09-2019 - |
Frage
Ich versuche, das „timeago“ zu berechnen, und ich bin mit einem descrepancy in meinem Code kommen.
Mit Heutiges Datum
7. November 2010
Wenn ich 1. September 2010 dann sowohl mein .NET-Code und mein JS-Code sagen "2 Monate"
Wenn ich 31. August 2010 dann meine .NET-Code sagt "3 Monate" und meine JS-Code sagt bei "2 Monate"
Diese Diskrepanz Aufenthalt bis zur 9. August 2010 .
Grundsätzlich ist die DATEDIFF „aus“ ist ab dem 10. August -. 31. August, basierend auf heutigen Datum vom 7. November
Hier ist das JavaScript (aus " timeago " Plugin)
var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
seconds < 90 && substitute($l.minute, 1) ||
minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
minutes < 90 && substitute($l.hour, 1) ||
hours < 24 && substitute($l.hours, Math.round(hours)) ||
hours < 48 && substitute($l.day, 1) ||
days < 30 && substitute($l.days, Math.floor(days)) ||
days < 60 && substitute($l.month, 1) ||
days < 365 && substitute($l.months, Math.floor(days / 30)) ||
years < 2 && substitute($l.year, 1) ||
substitute($l.years, Math.floor(years));
Und hier ist mein .NET-Code (von mir geschrieben)
Public Function ToDuration(ByVal dt As Date?, _
Optional ByVal suffixAgo As String = Nothing) As String
If Not dt Is Nothing Then
Dim theDate As Date = dt
Dim SecondsAppart As Integer = DateDiff(DateInterval.Second, theDate, Now)
Dim output As String
If SecondsAppart < 86400 Then
Select Case SecondsAppart
Case Is <= 59 : output = "less than a minute " & suffixAgo
Case Is <= 119 : output = "about a minute " & suffixAgo
Case Is <= 3599 : output = DateDiff(DateInterval.Minute, theDate, Now) & " minutes " & suffixAgo
Case Is <= 7199 : output = "about an hour " & suffixAgo
Case Else : output = DateDiff(DateInterval.Hour, theDate, Now) & " hours " & suffixAgo
End Select
Else
Dim DaysAppart As Integer = DateDiff(DateInterval.Day, theDate, Now)
Select Case DaysAppart
Case Is <= 1 : output = "yesterday"
Case Is <= 30 : output = DateDiff(DateInterval.Day, theDate, Now) & " days " & suffixAgo
Case Is <= 60 : output = "about a month " & suffixAgo
Case Is <= 365 : output = DateDiff(DateInterval.Month, theDate, Now) & " months " & suffixAgo
Case Is <= 730 : output = "about a year " & suffixAgo
Case Else : output = DateDiff(DateInterval.Year, theDate, Now) & " years " & suffixAgo
End Select
End If
Return output
Else
Return String.Empty
End If
End Function
Also das Problem, das ich habe, ist ein Grund sowie ein logistischen ein.
- Welcher Code „richtig“ ist, wenn es um DatDiff kommt? ( IE: Ist 2 Monate und 14 Tage als 2 Monate oder 3 )
- Was ist der beste Weg, um sie entsprechend zu ausrichten?
Lösung
Made einige Annahmen und hatte es in C # zu schreiben, aber diese Version des Codes gibt mir 2 Monate für den 31. August und 3 Monate für 9. August
public static string ToDuration(DateTime dt, string suffixAgo)
{
string output;
DateTime theDate;
if (dt == null)
{
output = "now";
}
else
{
theDate = dt;
TimeSpan DateInterval = DateTime.Now - theDate;
int SecondsAppart = Convert.ToInt32(Math.Floor(DateInterval.TotalSeconds));
if ((SecondsAppart < 86400))
{
if (SecondsAppart < 59)
output = ("less than a minute " + suffixAgo);
else if (SecondsAppart < 119)
output = ("about a minute " + suffixAgo);
else if (SecondsAppart < 3599)
output = string.Format("{0} minutes {1}", Math.Floor(DateInterval.TotalMinutes), suffixAgo);
else if (SecondsAppart < 7199)
output = "about an hour " + suffixAgo;
else
output = string.Format("{0} hours {1}", Math.Floor(DateInterval.TotalHours), suffixAgo);
}
else
{
int DaysAppart = Convert.ToInt32(DateInterval.TotalDays);
if (DaysAppart <= 1)
output = "yesterday";
else if (DaysAppart < 30)
output = string.Format("{0} days {1}", Math.Floor(DateInterval.TotalDays), suffixAgo);
else if (DaysAppart < 60)
output = "about a month " + suffixAgo;
else if (DaysAppart < 365)
output = string.Format("{0} months {1}", Math.Floor(DateInterval.TotalDays/30), suffixAgo);
else if (DaysAppart < 730)
output = ("about a year " + suffixAgo);
else
output = string.Format("{0} year {1}", Math.Floor(DateInterval.TotalDays/365), suffixAgo);
}
}
return output;
}
Ich habe den Code aktualisiert und ich denke, Sie die Ergebnisse haben Sie jetzt erwarten sind. Hopefuly Dies wird helfen.
Cheers, Wagner.