Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting and pretty printing dates with jquery

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?

like image 958
Tauren Avatar asked Apr 01 '10 01:04

Tauren


People also ask

What is the format of new Date ()?

By default, when you run new Date() in your terminal, it uses your browser's time zone and displays the date as a full text string, like Fri Jul 02 2021 12:44:45 GMT+0100 (British Summer Time).


2 Answers

http://timeago.yarp.com/

like image 83
Dustin Laine Avatar answered Oct 26 '22 23:10

Dustin Laine


Check out prettyDate.

It's made by the same guy that does the jQuery Validation plugin.

like image 35
alex Avatar answered Oct 27 '22 00:10

alex