typo icon

Displaying Dates and Times Using JavaScript

Posted in , , , , Tue, 06 Feb 2007 00:28:00 GMT

Some considerations when displaying dates and times on a website include showing delta times, customized timezones and caching. Often it's nice to show a delta time like "10 minutes ago" or "5 days ago" to give readers a frame of reference instead of an absolute date. When the date is far enough in the past and an absolute date becomes desired, customizing the date to the user's timezone is useful. And if your site grows large enough that caching becomes useful, finding a way to display customized deltas and timezone information in a cacheable static page becomes an ideal solution.

JavaScript is an ideal solution for all three issues. With JavaScript you can place an absolute date in the web page and have the JS dynamically update it when the page is loaded. This can be used to calculate delta times and accommodate timezones as well. The result is that the page can embed the same date every time and thus becomes more cache-friendly.

The Typo blog engine (which runs this blog) comes with a useful MIT-licensed JavaScript in it's typo.js script. Just copy three of the JS date/time functions, wrap your dates with spans (using the appropriate class name and absolute date in the span title) and then call show_dates_as_local_time() when your page is finished loading. The two other functions you'll need are get_local_time_for_date(time) and distance_of_time_in_words(minutes). This is what I did for Planet Catalyst's Plagger theme a while back.

Although it's pretty easy to accommodate timezones, the Typo script doesn't do that. I've done this for some projects and might post some code in the future but it's not hard.

Customization and cacheability, two great advantages for using JavaScript to handle dates and times.

del.icio.us:Displaying Dates and Times Using JavaScript digg:Displaying Dates and Times Using JavaScript reddit:Displaying Dates and Times Using JavaScript spurl:Displaying Dates and Times Using JavaScript wists:Displaying Dates and Times Using JavaScript simpy:Displaying Dates and Times Using JavaScript newsvine:Displaying Dates and Times Using JavaScript blinklist:Displaying Dates and Times Using JavaScript furl:Displaying Dates and Times Using JavaScript fark:Displaying Dates and Times Using JavaScript blogmarks:Displaying Dates and Times Using JavaScript Y!:Displaying Dates and Times Using JavaScript smarking:Displaying Dates and Times Using JavaScript magnolia:Displaying Dates and Times Using JavaScript segnalo:Displaying Dates and Times Using JavaScript

2 comments

Comments

  1. http://hype-free.blogspot.com/ said about 1 month later:

    Hello. I did something similar for Blogger (but it can be used generally):

    http://hype-free.blogspot.com/2006/12/my-time.html

  2. jeyasamy@gmail.com said about 1 year later:

    How to display real time dynamically using java script code

    By Jeyasree

(leave url/email »)

   Comment Markup Help Preview comment