jquery.tablesorter addParser表示"7月26日"日期格式 [英] jquery.tablesorter addParser for "26 Jul" date format

查看:92
本文介绍了jquery.tablesorter addParser表示"7月26日"日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对日期进行排序,如下所示: 7月26日

I want to sort dates which look like this: 26 Jul

这是我添加解析器的尝试:

This is my attempt at adding a parser:

       var lMonthNames = {};
lMonthNames["Jan"] = "01";
lMonthNames["Feb"] = "02";
lMonthNames["Mar"] = "03";
lMonthNames["Apr"] = "04";
lMonthNames["May"] = "05";
lMonthNames["Jun"] = "06";
lMonthNames["Jul"] = "07";
lMonthNames["Aug"] = "08";
lMonthNames["Sep"] = "09";
lMonthNames["Oct"] = "10";
lMonthNames["Nov"] = "11";
lMonthNames["Dec"] = "12";

ts.addParser({
  id: 'monthDay',
  is: function(s) {
    return false;
  },
  format: function(s) {
    if (s.length > 0) {
      var date = s.match(/^(\d{1,2})[ ](\w{3})[ ](\d{4})$/);
      var m = lMonthNames[date[2]];
      var d = String(date[1]);
      if (d.length == 1) {d = "0" + d;}
      return '' + m + d;
    } else {
      return '';
    }
  },
  type: 'numeric'
});

但是Firebug控制台告诉我,日期为null var m = lMonthNames [date [2]]; 您能选择为什么它没有价值吗?

But the firebug console tells me date is null for var m = lMonthNames[date[2]]; Can you pick why it doesn't have a value?

推荐答案

因为您使用的正则表达式与您提供的数据不匹配.

Because the regular expression you have doesn't match the data you are giving it.

您匹配的是26 Jul 2010之类的东西,而不是26 Jul之类的东西.

Your's matches things like: 26 Jul 2010 and not things like 26 Jul.

要么更改表中的数据,要么将正则表达式更改为:

Either change the data in the table, or change the regular expression to be:

      var date = s.match(/^(\d{1,2})[ ](\w{3})$/);

这篇关于jquery.tablesorter addParser表示"7月26日"日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆