Post #122

An ASP version of time_since

10th January 2004, mid-afternoon | Comments (16)

A while back, Natalie Downe published a very helpful bit of PHP code called time_since, which represents dates as the number of years, months, days, hours and minutes that have passed since that date. For example: “2 days, 6 hours and 9 minutes ago” rather than the more traditional “Thurs 8th January 2003 at 07:00”.

I don’t use this function on my site’s main pages (because here the full dates play an important role in site structure and linking), however I do use it in my blogroll (coming soon) where a relative timestamp is more appropriate than an absolute one.

The benefits of a relative approach

What are the benefits of time_since? Well, dates represented in the “Thurs 8th January 2003 at 07:00” format demand a certain amount of mental gymnastics:

  1. Which time zone is that date in?
  2. What’s the offset of that timezone?
  3. What timezone am I in? (travellers don’t always know)
  4. What’s the difference between that timezone and my timezone?
  5. What’s the date today?
  6. What’s the time now?
  7. Oh god, Maths. Right, now, take the minutes off first… damn, that’s into a new hour, so 53 minutes… now count up from 7 to 13, so that’s 6 off that and… shit, how many minutes was it again?…

Compare that to, “Oh, it was posted a couple of days ago, that’s old” and the difference in usability is obvious.

To cover all my bases however, I code my blogroll’s datetime statements like this:

  1. <li>
  3. <br />
  4. <span title="Thurs 8th January 2003, 0700 +0000 UTC">2 days, 6 hours and 9 minutes ago<span>
  5. </li>
  6. Download this code: 122a.txt

ASP TimeSince()

The reason I bring all this up is that I’m in the midst of adapting an ASP-coded forum and I wanted to swap all the post dates over to this friendly new format.

Enter Cam McVey, a wonderful chap who is embarrassingly helpful when I get stuck with the finer points of ASP and VB. Here is Cam’s five minute conversion of time_since into ASP/VB:

  1. <%
  2. ' Very kindly knocked up by Cam McVey
  3. '
  5. ' Based on an original script by Natalie Downe
  6. '
  8. Function TimeSince(Original)
  9. Dim aChunks, aYear, aMonth, aWeek, aDay, aHour, aMinute
  10. Dim dictChunks, aKeys
  11. Dim Today, iSince
  12. Dim i, j
  13. Dim iSeconds, iSeconds2
  14. Dim sName, sName2
  15. Dim iCount, iCount2
  16. Dim sPrint
  17. Dim Locale
  19. If Not IsDate(Original) Then
  20. TimeSince = "!!!ERROR!!!"
  21. Exit Function
  22. Else
  23. Original = CDate(Original)
  24. End If
  26. Set dictChunks = Server.CreateObject("Scripting.Dictionary")
  28. dictChunks.Add "year", 60 * 60 * 24 * 365
  29. dictChunks.Add "month", 60 * 60 * 24 * 30
  30. dictChunks.Add "week", 60 * 60 * 24 * 7
  31. dictChunks.Add "day", 60 * 60 * 24
  32. dictChunks.Add "hour", 60 * 60
  33. dictChunks.Add "minute", 60
  34. aKeys = dictChunks.Keys()
  36. ' set Locale to US datetime format (for example)
  37. '
  38. varLocale = SetLocale(1033)
  40. Original = FormatDateTime(Original, 0)
  43. Today = Now()
  44. iSince = CLng(DateDiff("s", Original, Today))
  46. j = dictChunks.Count
  47. For i = 0 To j - 1
  48. iSeconds = CLng(dictChunks.Item(aKeys(i)))
  49. sName = aKeys(i)
  51. iCount = Int(iSince / iSeconds)
  53. If iCount <> 0 Then
  54. Exit For
  55. End If
  56. Next
  58. If iCount = 1 Then
  59. sPrint = sPrint & "1 " & sName
  60. Else
  61. sPrint = sPrint & iCount & " " & sName & "s"
  62. End If
  64. If i + 1 < j Then
  65. iSeconds2 = dictChunks.Item(aKeys(i + 1))
  66. sName2 = aKeys(i + 1)
  68. iCount2 = Int((iSince - (iSeconds * iCount)) / iSeconds2)
  69. If iCount2 <> 0 Then
  70. If iCount2 = 1 Then
  71. sPrint = sPrint & ", 1 " & sName2
  72. Else
  73. sPrint = sPrint & ", " & iCount2 & " " & sName2 & "s"
  74. End If
  75. End If
  77. End If
  79. Set dictChunks = Nothing
  80. Erase aKeys
  82. TimeSince = sPrint
  84. End Function
  85. %>
  86. Download this code: 122b.txt

Datetime formats

TimeSince() is written to work on dates (not UNIX timestamps like its PHP counterpart). The datetime format of the string that you pass to TimeSince() has to match that of the datetime strings generated by your server. If it doesn’t (so say you pass a US-format date to TimeSince() running on a UK server) then you’ll get problems.

Luckily you can tell your server what country-format you’d like it to generate datetime strings in. For this we use LocaleID.

So if I have a UK-based server, and I pass TimeSince() a US-format date, I could use LocaleID(1033) to force the server to generate a US-format date when it compares ‘then’ to ‘now’.

Does that make sense? I hope so.

Location LocaleID Format 0 Format 1 Format 2 Format 3
UK 2057 10/01/2004 14:48:49 10 January 2004 10/01/2004 14:48:49
USA 1033 1/10/2004 2:48:49 PM Saturday, January 10, 2004 1/10/2004 2:48:49 PM

(The relevant lines to deal with this are marked up in the ASP script.)

And that’s that, I hope someone finds it useful. I’m wondering, though, what you guys and gals think of this ‘time since’ presentational method? Do you like it? Is it helpful? Or is it a pain? Be honest.

Jump up to the start of the post

Comments (16)

Jump down to the comment form ↓

  1. Dris:

    Quite useful.

    I never really liked looking at dates on blogs and other periodically-oriented sites. However, a function like time_since never occurred to me...

    Posted 2 hours, 48 minutes after the fact
  2. Nicole:

    In typical overanalyzing fashion, I think "so was that two days since the poster's time or two days ago relative to my time." Then, I think, "So was that January 8th or 9th that it was posted? And what time zone are they in?" Either way, I think I like to know the exact date with a helpful abbreviation of the time zone/locale.

    Or everyone could just have a handy image at top like you.

    Posted 3 hours, 29 minutes after the fact
    Inspired: ↓ Dunstan
  3. Dunstan:

    "so was that two days since the poster's time or two days ago relative to my time."

    2 days = 48 hours. It'd be the same regardless of which side you look at it from. It was updated '48 hours ago'.

    "And what time zone are they in?"

    That's it, it doesn't matter what timezone they're in - minutes and hours are the same everywhere, so 2 hours ago will be 2 hours ago everywhere. The _time_ will be different in different places, sure, but '2 hours ago' is a universal construct.

    I could phone someone up in Oz and say "What were you doing 2 hours ago?", and they'd be able to tell me. But if I ask "What were you doing at 3pm?" then they might think, "My 3pm, or your 3pm? And when was your 3pm? Where are you phoning from? The UK? The UK is behind us, right?"

    You see how this simplifies things?

    Where this really comes into it's own is on the blogrolls - go see simon's "Blogs I read" [1] you can quickly tell how long ago a blog was updated.

    For example, one current entry there reads:

    Dunstan's blog
    2 hours, 32 mins ago

    I guess this will suit some people's brains and not others - we all work differently :o)


    Posted 4 hours, 44 minutes after the fact
    Inspired by: ↑ Nicole
    Inspired: ↓ Nicole
  4. Zelnox:

    People do not use Swatch time, eh? Funny. <(^_^);;;

    Anyway, I find time since useful, although I would not benefit from it much. (I do not bother with the math, haha. If I did not read it, it means it is new for me). But if you want to use the usability perspective, why not use something more visual, say colours or symbols. Users won't even have to read the numbers. For instance, let us use a blog roll:
    green : updated today
    yellow : 1 half-week > updated >= yesterday
    orange : 1 week > updated >= 1 half-week
    red : updated >= 1 week

    Hmm, I'm writing w/ the panel next to me and it could be useful if it used relative a relative approach you brought to our attention.(I find fishy; I often miss updates yet I check many times daily). I think I see it better now and it would be useful for many people. Still, I rather be able to track what I did read and what I did not read regardless of time. I suppose that the concept of time is only relevant when we take into consideration its time of effect (as in, Dunstan giving away free apple pies on a certain date, and it would only matter if we have to respond on that date, hehe). I may be off-topic. >_<

    PS: The textarea toggle size is new? Amazing. (^_^)// <--applause

    Posted 6 hours, 31 minutes after the fact
  5. Nicole:

    "That's it, it doesn't matter what timezone they're in - minutes and hours are the same everywhere, so 2 hours ago will be 2 hours ago everywhere."

    Sure, that's what rational Mr. Dunstan would say. :) And I know, but my mind doesn't work like that. I want to know the exact time and the time zone. I like to know time zones -- even when it's one time zone away. Don't worry, I don't even make sense to myself.

    Posted 11 hours, 43 minutes after the fact
    Inspired by: ↑ Dunstan
    Inspired: ↓ Dunstan
  6. Scott Johnson:

    I like the idea of using "time since" on websites. Since we're all in different parts of the world, it's the only way to truly give a useful time without resorting to timezones. I prefer this method because timezones require the user to (a) know his timezone and (b) choose it from a menu. I find that it's a better user experience when I don't have to configure anything, and "time since" makes that possible.

    Posted 14 hours, 5 minutes after the fact
  7. Dunstan:

    Hee hee, this is great though, I'd never have known this. I just presumed everyone would prefer the relative method.

    Always nice to be reminded that it's dangerous to presume your choices will be everyone elses choices.

    I actually find the use of the relative method annoying at times. On Simon's blog [1] he has the relative date at the top of the post, and the specific date at the bottom - quite often I've felt a twinge of annoyance as I've had to scroll down to see _exactly_ when it was posted. So I can see where you're coming from.

    Hopefully by adding in the specific date info into the title attribute as I mention in the post, people such as yourself can still get their fix of precision when I implement it in my blogroll :o)

    Thanks for not giving up and saying "I suppose so" Nicole, it really is so helpful to hear your point of view.


    Posted 22 hours, 15 minutes after the fact
    Inspired by: ↑ Nicole
  8. Matt:

    Also, as well as the things mentioned previously, it can also be insightful to see what time, for them, a person posted. "Why on earth was person X posting about the relative density of egg yolks on a web design blog at 4:30am?".

    One solution may be to display both formats and provide the user with a means to customise the view to their tastes, be it using the server side scripting language, or javascript, depending on your content publishing system.

    Posted 1 day, 18 hours after the fact
    Inspired: ↓ Dunstan
  9. Matthew:

    Could you possibly put some content inside the iframe for those of us who disable iframes (because they're mostly adverts :) )? Thanks.

    Posted 3 days, 5 hours after the fact
    Inspired: ↓ Dunstan
  10. Dunstan:

    I shouldn't even be using iframes actually mathew (they're not allowed in this version of xhtml), I'll be replacing them with styled-divs soon.

    Sorry for the trouble!

    Posted 3 days, 5 hours after the fact
    Inspired by: ↑ Matthew
  11. Dunstan:

    Actually Matt, in making my blogroll I realised that it's not possible to tell when a person's site was updated in _their_ time - all the weblogs and stuff gives you a UTC time and date and then provides a time-offset for each site update.

    So while someone might post about the relative density of egg yolks on a web design blog at 4:30am PST, the update time on my blogroll would appear as 12:30am UTC.

    Shame, but there we are.

    Posted 4 days, 19 hours after the fact
    Inspired by: ↑ Matt
    Inspired: ↓ Matt
  12. Matt:

    True, I didn't think of that. I was really refering to reading someone's actual blog rather than getting updates from a blogroll.

    Still, I think the point holds. When you read the blogroll, it alerts you to the fact that someone has made a new post relative to your time, when you then visit their site to view the whole post, or comment on it, you can see what the relative time for them was from their local timestamp.

    Posted 1 week, 1 day after the fact
    Inspired by: ↑ Dunstan
    Inspired: ↓ Dunstan
  13. Dunstan:

    Yup, that makes perfect sense Matt, and that's the way I'd like to see it. (Hopefully, that's how it works here.)

    Posted 1 week, 1 day after the fact
    Inspired by: ↑ Matt
  14. DarkBlue:

    Perls of wisdom indeed. ;-)

    I'll add to the collective with a Perl version of the time_since() function:

    Regards to all.

    Posted 1 month, 1 week after the fact
  15. Elaine:

    I am very excited to see that you have adapted this script to ASP. I have been looking for a script to do this instead of writing it myself. It will be extremely helpful to me. I write my baby's blog and instead of using dates and times I'd like to say how old she was at the time the blog was written! I'm going to implement it right now! Thanks!!!


    Posted 8 months, 3 weeks after the fact
  16. Andrei B:


    I've done some work today with relative and friendly date and time (in PHP.) I have to admit that your site inspired me to do this.

    Anyway, you can read up on what I've been doing @

    Thanks again!

    Posted 1 year after the fact

Jump up to the start of the post

Add your comment

I'm sorry, but comments can no longer be posted to this blog.