Formatting and pretty printing dates with jquery

Posted by Tauren on Stack Overflow See other posts from Stack Overflow or by Tauren
Published on 2010-04-01T01:46:10Z Indexed on 2010/04/01 1:53 UTC
Read the original article Hit count: 564

Filed under:
|
|
|

I need to display dates in a couple different ways in an app built with jquery.

In some situations, I need the typical "yyyy-mm-dd hh:mma" type of formatting, with all of it's different permutations. In other cases, I need to show dates "pretty printed" similar to how StackOverflow does them:

  • 5 seconds ago
  • 12 minutes ago
  • 3 hours ago
  • yesterday
  • 2 days ago

My application already uses JQuery UI DatePicker which includes a formatDate() function, but as far as I can tell, there is no way to use it outside of the datepicker. I want to format dates that aren't associated with a datepicker. Is it possible to do this using DatePicker?

The DateJS library can parse dates like "12 minutes ago", but as far as I can tell, it cannot take a Date object and format strings like this. It can format the typical "yyyy-mm-dd" types of formats. This library seems pretty heavy as well.

John Resig's Pretty Dates looks like it can provide the pretty printing ("2 hours ago"), but it doesn't do the standard formatting.

Is there not a single plugin that can do all of this? Is there a way to leverage the DatePicker code so I don't have to load multiple codebases that do the same things?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery