I want to display a "date/time submitted" value much the same way as Stack Overflow does
e.g.* 2 hours ago * 3 days ago * a month ago
I see extensive answers on how to do this in PHP but can someone help me with the VB version?
I want to display a "date/time submitted" value much the same way as Stack Overflow does
e.g.* 2 hours ago * 3 days ago * a month ago
I see extensive answers on how to do this in PHP but can someone help me with the VB version?
You need a DateTime structure
you simply substract [now] - [original message time] = difference time
you will need the DateTime.Substract method
First calculate the exact time since submission:
Dim t As TimeSpan = DateTime.Now - submittedTime
Then you decide on what intervals you want to use for displaying the result. For example:
If t.TotalSeconds < 60 Then
display = "Less than a minute ago"
ElseIf t.TotalMinutes < 60 Then
display = t.Minutes.ToString() + " minutes ago"
ElseIf ...
... and so on
End If
What you're looking for is the TimeSpan structure. This structure stores a span of time (as the name suggests). It is the type returned when you subtract two DateTime structures
Dim start as DateTime = DateTime.Now
...
' Some time after the start
Dim span = DateTime.Now - start
All the way back on question 11, Jeff posted the code they use here on stackoverflow. It's in c#, but the conversion to vb.net should be pretty easy. There's heaps of other good suggestions in that question too.
Personally, I've used Sam Allen's code to get prettydates before. He modeled it off some code that John Resig wrote, so it's good quality stuff. Once again, c#, but it converts to vb.net pretty easily.
I've posted a blog post to do just this. However it is in C#, but you can easily convert into vb.net http://blog.nirandas.com/post/displaying-time-in-relative-format.aspx