phstc/jquery-dateformat

使用 JavaScript 格式化日期输出的 jQuery 插件。

资助包维护!
phstc

安装数: 19 998

依赖关系: 1

建议者: 0

安全: 0

星标: 678

关注者: 40

分支: 240

开放性问题: 18

语言:JavaScript


README

jquery-dateFormat - 使用 JavaScript 格式化日期输出的 jQuery 插件 - 体积小于 5kb,jquery-dateFormat 是可用的最小的日期格式化库!

build passing

安装

下载最新的 jquery.dateFormat.js 或 jquery.dateFormat.min.js。

您可以使用 不带 jQuery 的 jquery-dateFormat。您只需导入上面的 dateFormat.js,然后使用 DateFormat.format(...) 而不是 $.format(...) 进行格式化。

如果您使用 jQuery Validate 插件,则必须使用不带 jQuery 的 jquery-dateFormat。

格式化模式

格式化模式基于 java.text.SimpleDateFormat

日期和时间模式

  • yy = 短年份
  • yyyy = 长年份
  • M = 月份 (1-12)
  • MM = 月份 (01-12)
  • MMM = 月份缩写 (Jan, Feb ... Dec)
  • MMMM = 长月份 (January, February ... December)
  • d = 天 (1 - 31)
  • dd = 天 (01 - 31)
  • ddd = 星期几的名称 (Monday, Tuesday ... Sunday)
  • E = 星期几的缩写 (Mon, Tue ... Sun)
  • D - 序数天 (1st, 2nd, 3rd, 21st, 22nd, 23rd, 31st, 4th...)
  • h = 上午/下午的小时 (0-12)
  • hh = 上午/下午的小时 (00-12)
  • H = 一天中的小时 (0-23)
  • HH = 一天中的小时 (00-23)
  • mm = 分钟
  • ss = 秒
  • SSS = 毫秒
  • a = AM/PM 标记
  • p = 上午/下午标记

期望输入日期格式

  • 1982-10-15T01:10:20+02:00
  • 1982-10-15T01:10:20Z
  • Thu Oct 15 01:10:20 CET 1982
  • 1982-10-15 01:10:20.546
  • Thu Oct 15 1982 01:10:20 GMT-0800 (PST)
  • Thu Oct 15 1982 01:10:20 GMT+0800 (中国标准时间)
  • Thu Oct 15 1982 01:10:20 GMT+0200 (西欧夏令时)
  • 1982-10-15CET01:10:20
  • JavaScript: new Date().getTime()

一些示例.

用法

 <script>
   document.write($.format.date("2009-12-18 10:54:50.546", "Test: dd/MM/yyyy"));
   document.write($.format.date("Wed Jan 13 10:43:41 CET 2010", "dd~MM~yyyy"));
 </script>

输出

 => Test: 18/12/2009
 => 13~01~2010

使用 CSS 类进行格式化

  <span class="shortDateFormat">2009-12-18 10:54:50.546</span>
  <span class="longDateFormat">2009-12-18 10:54:50.546</span>
  jQuery(function() {
      var shortDateFormat = 'dd/MM/yyyy';
      var longDateFormat  = 'dd/MM/yyyy HH:mm:ss';

      jQuery(".shortDateFormat").each(function (idx, elem) {
          if (jQuery(elem).is(":input")) {
              jQuery(elem).val(jQuery.format.date(jQuery(elem).val(), shortDateFormat));
          } else {
              jQuery(elem).text(jQuery.format.date(jQuery(elem).text(), shortDateFormat));
          }
      });
      jQuery(".longDateFormat").each(function (idx, elem) {
          if (jQuery(elem).is(":input")) {
              jQuery(elem).val(jQuery.format.date(jQuery(elem).val(), longDateFormat));
          } else {
              jQuery(elem).text(jQuery.format.date(jQuery(elem).text(), longDateFormat));
          }
      });
  });

输出

 => 18/12/2009
 => 18/12/2009 10:54:50

漂亮的日期格式化

jQuery.format.prettyDate(value) 返回表示日期的字符串

  • value = 表示 ISO 时间或日期的字符串、毫秒或 JavaScript 日期对象
 jQuery.format.prettyDate(new Date())             // => "just now"
 jQuery.format.prettyDate(new Date().getTime())   // => "just now"
 jQuery.format.prettyDate("2008-01-28T20:24:17Z") // => "2 hours ago"
 jQuery.format.prettyDate("2008-01-27T22:24:17Z") // => "Yesterday"
 jQuery.format.prettyDate("2008-01-26T22:24:17Z") // => "2 days ago"
 jQuery.format.prettyDate("2008-01-14T22:24:17Z") // => "2 weeks ago"
 jQuery.format.prettyDate("2007-12-15T22:24:17Z") // => "more than 5 weeks ago"

toBrowserTimeZone

jQuery.format.toBrowserTimeZone(value, format) 转换为浏览器时区。

  • value = 表示日期的 ISO 时间字符串 ("2013-09-14T23:22:33Z") 或表示默认 JAXB 格式的 java.util.Date 字符串 ("2013-09-14T16:22:33.527-07:00") 或表示 Unix 时间戳 (Sat Sep 14 2013 16:22:33 GMT-0700 (PDT)) 或 JavaScript 日期对象。
  • format = 所有对 jQuery.format.date 有效的输入格式都适用于此方法。默认格式为 MM/dd/yyyy HH:mm:ss。

有效的输入格式

 var date1 = "2013-09-14T23:22:33Z";
 var date2 = "2013-09-14T16:22:33.527-07:00";
 var date3 = "Sat Sep 14 2013 16:22:33 GMT-0700 (PDT)";

 $.format.toBrowserTimeZone(date1)
 $.format.toBrowserTimeZone(date2)
 $.format.toBrowserTimeZone(date3)

开发

要编译 jquery-dateFormat(生成 dist 文件)

npm run compile

测试

测试是用 Jasmine 编写的。要使用 PhantomJS 运行测试套件,请运行 npm run test。要在默认浏览器中运行测试套件,请运行 npm run test:browser

许可证

jquery-dateFormat遵循MIT许可证发布。

您想改进jquery-dateFormat吗?

欢迎您做出贡献,并将它们作为pull request发送。

贡献者

感谢所有贡献者