最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Formatting and pretty printing dates with jquery - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Apr 1, 2010 at 1:46 TaurenTauren 27.3k44 gold badges133 silver badges169 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

http://timeago.yarp./

Check out prettyDate.

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

发布评论

评论列表(0)

  1. 暂无评论