简体   繁体   English

使用jquery格式化和漂亮打​​印日期

[英]Formatting and pretty printing dates with jquery

I need to display dates in a couple different ways in an app built with jquery. 我需要在使用jquery构建的应用程序中以几种不同的方式显示日期。

In some situations, I need the typical "yyyy-mm-dd hh:mma" type of formatting, with all of it's different permutations. 在某些情况下,我需要典型的“yyyy-mm-dd hh:mma”类型的格式,所有这些都是不同的排列。 In other cases, I need to show dates "pretty printed" similar to how StackOverflow does them: 在其他情况下,我需要显示类似于StackOverflow的日期“漂亮打印”:

  • 5 seconds ago 5秒前
  • 12 minutes ago 12分钟前
  • 3 hours ago 3小时前
  • yesterday 昨天
  • 2 days ago 2天前

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. 我的应用程序已经使用了包含formatDate()函数的JQuery UI DatePicker,但据我所知,没有办法在datepicker之外使用它。 I want to format dates that aren't associated with a datepicker. 我想格式化与datepicker无关的日期。 Is it possible to do this using DatePicker? 是否可以使用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. DateJS库可以解析像“12分钟前”这样的日期,但据我所知,它不能采用Date对象并格式化这样的字符串。 It can format the typical "yyyy-mm-dd" types of formats. 它可以格式化典型的“yyyy-mm-dd”格式。 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. John Resig的漂亮日期看起来可以提供漂亮的打印(“2小时前”),但它没有进行标准格式化。

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? 有没有办法利用DatePicker代码,所以我不必加载多个执行相同操作的代码库?

Check out prettyDate . 看看prettyDate

It's made by the same guy that does the jQuery Validation plugin. 它是由jQuery Validation插件的同一个人制作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM