There are many robust JavaScript libraries out there for handling timestamps, but I had a situation requiring a lightweight solution to display dates returned from a JSON request. I wanted human-readable, contextual time to indicate the proximity to an expiration. Something to the effect of, “Feb 3, 2010 (in 5 days)”, or, “Jan 1, 2010 (30 days ago)”.

This implementation doesn’t handle language localization or customizing messages like optionally breaking down to also include years or decades. But it does contain nested ugliness with magic numbers ahoy! I didn’t say it was elegant; just lightweight (see: took less than 10 minutes to create, even with clever pluralization of each unit.)

Edit from 2016: In all fairness, Moment.js is your best bet.