jquery.tablesorter addParser for “26 Jul” date format

拥有回忆 提交于 2019-12-13 07:07:55

问题


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'
});

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?


回答1:


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

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})$/);


来源:https://stackoverflow.com/questions/4893505/jquery-tablesorter-addparser-for-26-jul-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!