Welcome to Dovetail Software Blogs : Sign in | Join | Help
Pretty Dates in JavaScript

A common way of expressing dates nowadays is to represent them as an approximate age.

For example, rather than saying something happened at 5/5/2008 4:21:01 PM, we can say it happened one minute ago.

I wanted to use this in DovetailAgent to show the age of the Search indexes. A little Googling brought me to John Resig's implementation of prettyDate() in JavaScript.

I tried it out, it was OK.

Looking through the comments on John's post, I saw that Dean Landolt posted an updated function that I liked better.

I tried it, but it still had a few issues.

Finally, Zach Letterman commented that he updated Dean's script, and posted a newer version.

Success! Exactly what I was looking for.

 

Now when I see my search results, I can also see how current my search data is, in an easy to consume format, without having to do any date arithmetic in my head:

 

search

 

Other uses?

I'm wondering if this would also be useful when displaying dates in other places in Dovetail applications, such as the case history. Do you care that John logged a note at 5/2/2008 1:20:01 PM, or do you care that it happened 3 days ago? Perhaps the activity log still shows the exact date/time, so that data can be easily retrieved, if needed.

Your thoughts?

Posted: Monday, May 05, 2008 5:58 PM by gsherman

Comments

Gary Storey said:

I don't know if it would meet your needs or not but you might want to check out http://www.datejs.com/ for an open source date javascript library.

Some interesting examples from their site:

// Add 5 days to today

Date.today().add(5).days();

// Is today Friday?

Date.today().is().friday();  // true|false

// Get a date 3 days from now

(3).days().fromNow();

// 6 month ago

(6).months().ago();

// 12 weeks from now

var n = 12;

n.weeks().fromNow();

# May 6, 2008 10:39 AM

gsherman said:

Thanks Gary - that looks pretty slick.

It doesn't seem to have the approximate age stuff I was looking for for the task at hand, but it does seem to have some useful stuff that may be useful at some point.

Thanks for sharing!

# May 6, 2008 10:50 AM

Kent Valentine said:

I'm all for pretty dates in the case history, but can you make this a user selectable option?

# May 10, 2008 9:29 AM

gsherman said:

Kent - not sure what you mean. What would be user selectable? What are your reasons for such configuration?

# May 12, 2008 10:15 AM