Peel me a Grape :: We make things work

We Make Things Work :: Blog

As a follow up to my post about converting a date to words like 5 hours ago, using javascript.

Here’s how you can use it.

Markup your dates something like this.

<span class="gmttime" title="Aug, 29 2008 22:44:41 GMT">29th Aug 2008</span>

Then at bottom of your document add this script

<script>TimeInWordsHelper.convertBySelector('.gmttime');</script>

and viola – your span will be converted to something like:

<span class="gmttime" title="29th Aug 2008">2 days ago</span>

Here’s an example, some tests and the source for you to reuse/fork

Some things to think about:

  • consider this as overkill/inappropriate – and just use plain dates! Less is more…
  • restrict how far in the past you convert dates. 2 minutes ago is useful, perhaps when it’s months ago, knowing the specific date is more valuable.
  • convert the date in a different way, eg 99designs – 5 days, 4 hours
  • use a PeriodicalExecutor to re refresh the dates, useful on an interface that keeps user on the same view for a long time.
  • when dates are converted they will take up a different amount of space on the screen – avoid your layout changing drastically by careful use of css.
  • avoid flickering as dates get converted, perhaps keep them hidden until converted, or use effects to make a smoother transition
blog comments powered by Disqus