Note that there are some explanatory texts on larger screens.

plurals
  1. PODate Calculation Discrepency (JavaScript VS .NET)
    text
    copied!<p>I'm trying to calculate the "timeago" and I'm coming up with a descrepancy in my code.</p> <p>Using Today's Date</p> <blockquote> <p>November 7, 2010</p> </blockquote> <p>If I use <strong>September 1, 2010</strong> then both my .NET code and my JS code say "2 months"</p> <p>If I use <strong>August 31, 2010</strong> then my .NET code says "3 months" and my JS code says at "2 months"</p> <p>This discrepancy stays until <strong>August 9, 2010</strong>.</p> <p>Basically the dateDiff is "off" from August 10 - August 31 based on todays date of November 7.</p> <p>Here's the JavaScript (taken from "<a href="https://github.com/rmm5t/jquery-timeago" rel="nofollow">timeago</a>" plugin)</p> <pre><code> var words = seconds &lt; 45 &amp;&amp; substitute($l.seconds, Math.round(seconds)) || seconds &lt; 90 &amp;&amp; substitute($l.minute, 1) || minutes &lt; 45 &amp;&amp; substitute($l.minutes, Math.round(minutes)) || minutes &lt; 90 &amp;&amp; substitute($l.hour, 1) || hours &lt; 24 &amp;&amp; substitute($l.hours, Math.round(hours)) || hours &lt; 48 &amp;&amp; substitute($l.day, 1) || days &lt; 30 &amp;&amp; substitute($l.days, Math.floor(days)) || days &lt; 60 &amp;&amp; substitute($l.month, 1) || days &lt; 365 &amp;&amp; substitute($l.months, Math.floor(days / 30)) || years &lt; 2 &amp;&amp; substitute($l.year, 1) || substitute($l.years, Math.floor(years)); </code></pre> <p>And here's my .NET code (written by me)</p> <pre><code> 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 &lt; 86400 Then Select Case SecondsAppart Case Is &lt;= 59 : output = "less than a minute " &amp; suffixAgo Case Is &lt;= 119 : output = "about a minute " &amp; suffixAgo Case Is &lt;= 3599 : output = DateDiff(DateInterval.Minute, theDate, Now) &amp; " minutes " &amp; suffixAgo Case Is &lt;= 7199 : output = "about an hour " &amp; suffixAgo Case Else : output = DateDiff(DateInterval.Hour, theDate, Now) &amp; " hours " &amp; suffixAgo End Select Else Dim DaysAppart As Integer = DateDiff(DateInterval.Day, theDate, Now) Select Case DaysAppart Case Is &lt;= 1 : output = "yesterday" Case Is &lt;= 30 : output = DateDiff(DateInterval.Day, theDate, Now) &amp; " days " &amp; suffixAgo Case Is &lt;= 60 : output = "about a month " &amp; suffixAgo Case Is &lt;= 365 : output = DateDiff(DateInterval.Month, theDate, Now) &amp; " months " &amp; suffixAgo Case Is &lt;= 730 : output = "about a year " &amp; suffixAgo Case Else : output = DateDiff(DateInterval.Year, theDate, Now) &amp; " years " &amp; suffixAgo End Select End If Return output Else Return String.Empty End If End Function </code></pre> <p>So the problem I'm having is a fundamental one as well as a logistic one.</p> <ol> <li>Which code is "right" when it comes to DateDiff? (<em>IE: Is 2 months and 14 days considered 2 months or 3?</em>)</li> <li>What is the best way to get them to line up accordingly?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload