Humane dates with unobtrusive javascript…

I stumbled upon a little piece of code from John Resig (of jQuery fame) that fuzzies up dates on the fly. This script hits on some of my favorite patterns: humanity in interface design, unobtrusive javascript, and microformats, with both human and machine-readable dates.

But I’m none too fond of the the design — the default-operator cascade thing seems a bit obfuscated. An alternative was proposed in the comments, so I picked up on that and extended it a bit. I hacked it up to handle timezones properly and deal with future dates. Oh, and bloated it with a few extra lines to handle more than a few weeks worth of a date ranges.

Now it will break out the length of time from now at every level: minutes, hours, days, weeks, months, years and even centuries, just for good measure. It will return yesterday or tomorrow; last week or next week; last year; just now; yada yada yada.

Have a look at humane.js for more.

5 Responses to “Humane dates with unobtrusive javascript…”

  1. John Resig - JavaScript Pretty Date Says:

    Kramer auto Pingback[…] So I worked out the UTC issue — all you have to do is add Date.getTimezoneOffset() * 60000) to the seconds diff line. I think that’s all there is to it. I’ve written it up here: http://deanlandolt.com/archives/163 […]

  2. Restamon Says:

    I just found the original at John’s site, and decided to try it out. I have a slightly different date format (”2008-01-28T20:24:17-08:00″ vs. “2008-01-28T20:24:17Z”), so I ran into a little trouble, but then I saw your comments about having worked out the UTC issue. I still have the same problem with yours, for which I have written a small work around, but I like the changes and enhancements that you have made … I think I’m going to go with your version.

    Thanks!

  3. Restafarian.org Says:

    ISO 8601 Date Formatting, Client-side…

    I went searching through the Internet looking for a script to convert my new ISO 8061 dates back into something a little nicer to look at, and I found almost exactly what I was looking for on John Resig’s site: JavaScript Pretty Date. The only li…

  4. Donnie Clapp Says:

    You have a comma at the end of line 63, after the last element in the array you’re creating. This works, but as I understand it isn’t recommended.

    Great script, btw, and thanks for carrying the jQuery plugin part through to your version.

    –dhc–

  5. zachleat.com {web} » Yet Another Pretty Date JavaScript Says:

    Kramer auto Pingback[…] more I used Dean Landolt’s script, the more problems I began to see with his implementation. It was a good start, but definitely had […]

Leave a Reply

Rambling semi-coherently since 2006…

Close
E-mail It